iCloud - How to save archive containing array of custom objects - ios

I have developed a small app that stores locally in iOS through archiving an array of custom objects containing:
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *dateCreated;
#property (nonatomic, copy) NSString *desc;
#property (nonatomic, copy) NSString *url;
I want to sync said archive using iCloud and I believe the recommended mechanism is through a UIDocument subclass.
All UIDocument examples I found utlilized a single instance with 1 single NSString, so I am a little confused how to go about syncing a whole array of custom objects but utilizing UIDocument (like I do today locally through NSCoding).
Should I create an array of UIDocument objects containing the properties listed above, should I create an instance of UIDocument containing 1 instance of the data object described above and then create an array containing all the instances, or should 1 single UIDocument contain the complete array of custom objects ?
I have done some research but I am still confused.
In the end I would need to sync just 1 file containing an array of said custom objects.
Thanks in advance for your help
What I have today is a custom class as described above with 4 strings called Snippet and in my Root view Controller I have an NSMutableArray called list where I add each new instance of that Snippet Class.
self.list = [[NSMutableArray alloc] init];
Snippet *newEntry = [[Snippet alloc] init];
[self.list addObject:newEntry];
Should I create an UI Document subclass that owns the array of custom objects ?

The example in the docs does indeed show a UIDocument subclass that just has one string, but it returns a NSData from -contentsForType:error:. You can store as many objects as you like in an NSData using an NSKeyedArchiver. Read Serializing Objects to learn how to encode objects using NSKeyedArchiver (and keep reading to learn how to get them back!).
Using your properties as an example...
#interface MyDocument : UIDocument
{
}
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *dateCreated;
#property (nonatomic, copy) NSString *desc;
#property (nonatomic, copy) NSString *url;
#end
#implementation MyDocument
//...
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:name forKey:#"name"];
[archiver encodeObject:dateCreated forKey:#"created"];
[archiver encodeObject:desc forKey:#"desc"];
[archiver encodeObject:url forKey:#"url"];
[archiver finishEncoding];
// release archiver if you're not using ARC
return data;
}
#end;
WARNING: I haven't compiled the code above, so no guarantees. This should serve as an example to illustrate using an archiver to store multiple objects in a single data object which you can return as your document's content.

Related

Fast NSData unarchiving

I create NSData from a class instance like this:
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:db];
[data writeToFile:#"/Users/.../db.data" atomically:true];
Then I need to load this instance:
NSURL* dbUrl = [[NSBundle mainBundle]URLForResource:#"db" withExtension:#"data"];
NSData* dbData = [NSData dataWithContentsOfURL:dbUrl];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
self.db = [NSKeyedUnarchiver unarchiveObjectWithData:dbData];
});
This loading lasts cca. 6 seconds for ~70MB file size. It is very slow! I tried to separate db instance into 2 different parts (~35MB) and load them like this:
self.db = [[Database alloc]init];
NSURL* dbUrl1 = [[NSBundle mainBundle]URLForResource:#"db1" withExtension:#"data"];
NSData* dbData1 = [NSData dataWithContentsOfURL:dbUrl1];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
self.db.nodes = [NSKeyedUnarchiver unarchiveObjectWithData:dbData1];
});
NSURL* dbUrl2 = [[NSBundle mainBundle]URLForResource:#"db2" withExtension:#"data"];
NSData* dbData2 = [NSData dataWithContentsOfURL:dbUrl2];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
self.db.trips = [NSKeyedUnarchiver unarchiveObjectWithData:dbData2];
});
With this way loading lasts cca. 3 seconds (2x faster). Do you know another ways to load it faster?
EDIT 1:
I'm not sure that it can help, but archived object has two NSMutableArray instances. The first one contains a lot of Node objects:
#interface DatabaseNode : NSObject <NSCoding>
#property (assign, nonatomic) NSInteger index;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) double lat;
#property (assign, nonatomic) double lon;
#end
And the second one contains a lot of Trip objects:
#interface DatabaseTrip : NSObject <NSCoding>
#property (assign, nonatomic) NSInteger index;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) NSInteger service;
#property (strong, nonatomic) NSMutableArray* departureTimes; //Contains NSDate
#end
Given that you need all the data in memory from the beginning of your app's session, you're going to have to go with a solution other than NSKeyedArchiver. NSKA is designed to archive complex object graphs and, thus, is optimized to solve a different problem.
In your case, I'd suggest a build phase that writes the static data to a file format that can be mapped into memory without parsing. Then, at runtime, map the file and have a pass that turns it into a minimal encapsulation in Foundation types.
For Strings, a string table like solution will work well enough. Dates can be a little bit trickier, maybe. I'd test to see if the time interval based creation methods are fast. If they are, then you can just store your dates as arrays (not NSArrays, but flat C arrays of NSTimeIntervals) in the mapped file and do a fixup pass.
Of course, you may also skip the fixup of some subset of data based on the access patterns in your app. If your users don't really look at all the dates at once, then there is no need to fix 'em up on load, but they can be created on demand.

Using JSQMessages framework with Core-data

This is just the Core-Data object for message:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Account;
#interface Message : NSManagedObject
#property (nonatomic, retain) NSNumber * read;
#property (nonatomic, retain) NSDate * sentDate;
#property (nonatomic, retain) NSString * text;
#property (nonatomic, retain) NSString * receiver;
#property (nonatomic, retain) NSString * sender;
#property (nonatomic, retain) Account *account;
#end
Then this is the Messages ViewController, load messages code:
- (void)loadMessages
messages = [[NSMutableArray alloc]initWithArray:_fetchedResultsController.fetchedObjects];
//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.
static NSString *senderId = #"2";
static NSString *senderDisplayName = #"a";
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
senderDisplayName:senderDisplayName
date:[NSDate distantPast]
text:#"helloloo"];
messages = [[NSMutableArray alloc]initWithObjects:message, nil];
//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.
Now I'm new to this framework https://github.com/jessesquires/JSQMessagesViewController , I went all over the framework docs and didn't find answer for my problem which is:
Can I use (NSFetchedResultsController) with this framework?
If so how can I use the (NSFetchedResultsController) in the JSQMessages CollectionView DataSource in the right way and return all my Messages from my core data?. I know how to use (NSFetchedResultsController) but not so sure how to use it with the CollectionView and with this framework.
It seem like the CollectionView data source/delegate methods only work with NSmutableArray and the app crashes when I do staff with (NSFetchedResultsController).
For example:
return [_fetchedResultsController.fetchedObjects objectAtIndex:indexPath.item];
VS:
return messages[indexPath.item];
When I use the _fetchedResultsController.fetchedObjects my app crashes.
So after thinking what to do I said ok what if I will convert my .fetchedObjects (NSarray) to be my (NSmutablearray) of the messages and then I got stack I just don't know how to do this the right way I need help.
All I need is just a way to return all my messages from my SQlite database to my app.
I also have looked at Parse example https://github.com/relatedcode/RealtimeChat , here but I want my own Code without having to pay to Parse.com and to be able to use Core-Data.
When I call this : JSQMessage I need some how to tell it to return all my objects in my fetch result controller *.fetechedobjects*.

NString value of custom object gets lost after segue (Objective C)

Heyo Guys
So I am more or less new to Objective C and got to a problem which I seem unable to solve.
I created a class called "Students" which all have a name surname etc. All those Students are put into a NSMutableArray (before they get created from JSON , that seems to work without a problem though). Then all the names of the students are put into a ListView. Afterwards when a name is clicked (segue passes the object), one should see the details of said student (i.e. his full name, his id, his street).
The problem is that all the string values of the student object seem to get lost.
I checked that all the student object work just fine. I think the problem lies at the #property in my student class.
Any comments and suggestions are appreciated
This is an excerpt of student.h As said only the string values get lost, the int (here the plz value) remains correct
#property (nonatomic, weak)NSString *lastname;
#property (nonatomic, weak)NSString *surname;
#property (nonatomic, weak)NSString *street;
#property (nonatomic, assign)int plz;
EDIT:
Here is where i parse my json.
for (NSDictionary *dic in jsonArray){
NSNumber *identity = [dic valueForKey:#"id"] ;
NSString *firstName = (NSString*) [dic valueForKey:#"first_name"];
NSString *lastName = (NSString*) [dic valueForKey:#"last_name"];
NSString *street = (NSString*) [dic valueForKey:#"street"];
NSNumber *plz = [dic valueForKey:#"plz"] ;
NSString *birth = (NSString*) [dic valueForKey:#"date_of_birth"];
NSArray *bills = dic[#"bills"];
NSArray *hours = dic[#"hours"];
NSLog(#"First %#",firstName);
Student *student = [[Student alloc]initWithLastName:lastName withSurname:firstName withStreet:street withPLZ:plz withOrt:#"Uitikon" withBirthDate:birth withOccupation:#"Schüler"];
[ApprenticeList addObject:student];
}
EDIT 2 :
I found out that the string values get lost even before the segue. All these objects are created in
ViewDidLoad
But in
prepareforsegue
all the values are allready null (except for the int) .So the only place where the student objects work is in
ViewdidLoad
#property (nonatomic, weak)NSString *lastname;
#property (nonatomic, weak)NSString *surname;
#property (nonatomic, weak)NSString *street;
change to
#property (nonatomic, strong)NSString *lastname;
#property (nonatomic, strong)NSString *surname;
#property (nonatomic, strong)NSString *street;
You can also use 'copy'. It will cause the setter for that property to create a copy of the object, otherwise it is identical to strong.

How to get Custom Objects from JSON in Xcode?

Let us say I have an NSObject Class Person.
#interface Person : NSObject
#property NSString *id;
#property NSString *name;
#property Address *billingAddress;
#end
#interface Address : NSObject
#property NSString *lane;
#property NSString *country;
#property NSString *zip;
#end
Now when I fetch the response from a URL, the response is in the form:
{
"response":
{
"Persons":[{"id":"2232","name":"John","Address":{"lane":"Adelaide Avenue","country":"Canada","zip":"45503"}}{"id":"3422","name":"Jane","Address":{"lane":"Victoria Avenue","country":"Australia","zip":"34903"}}]
}
}
I want to parse the response directly into objects without having to write a method to read and assign objects from NSDictionary. Is there are no objects to parse directly from the response to Object based on the Object parameters similar to "GSon" in Android.
EDIT:
I have used the below code to have generic class that does the job for strings without having to know about the object itself.
for (NSString *key in [dct allKeys]) {
if ([cat respondsToSelector:NSSelectorFromString(key)]) {
[cat setValue:[dct valueForKey:key] forKey:key];
}
}
There is no such magic, not even in Android's GSon!!!
Some where down the line you need to write code for converting JSON to your object.
You may create a generic class, or a method (just once) to convert all dictionary values to your object.
After some digging I did get a JSON framework that does exactly what I wanted - JSONModel.
We just need to specify Models and relationships and all the logic for converting JSON response to the models is handled by the framework. Very handy.
Basic usage :
Consider you have a JSON response like
{"id":"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
The corresponding model will be
#import "JSONModel.h"
#interface CountryModel : JSONMode
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* country;
#property (strong, nonatomic) NSString* dialCode;
#property (assign, nonatomic) BOOL isInEurope;
#end
We don't need to write additional code in the .m file to parse and assign values to the variables. Now to initialise the model from the response we just need to do the below
NSString* json = (fetch JSON here)
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];
The works well with complex data structures as well.

NSDictionary custom object store and retrieve

Trying to store value in NSDictionary and retrieve it
Objects
#import <Foundation/Foundation.h>
#class ATTTEstOBJ;
#interface ATTTEst : NSObject
#property (nonatomic, retain) NSString *string1;
#property (nonatomic, retain) NSString *string2;
#property (nonatomic, retain) ATTTEstOBJ *obj1;
#end
#interface ATTTEstOBJ : NSObject
#property (nonatomic, retain) NSString *string3;
#property (nonatomic, retain) NSString *string4;
#property (nonatomic, retain) NSString *array1;
#end
I know it needs to be encoded properly to save and retrieve values.but In this case it is a composite object and I have no idea, how to deal it with.
- (void) encodeWithCoder: (NSCoder *)coder
So TLDR , How to save the composite value into dictionary and retrieve it back
I want to store ATTTest into a dictionary and retrieve it back.
EDIT : Detailed explanation
ATTTEst *test=[[ATTTEst alloc]init];
test.string1=#"a";
test.string2=#"b";
ATTTEstOBJ *obj=[[ATTTEstOBJ alloc]init];
obj.string3=#"c";
obj.string4=#"d";
test.obj1=obj;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithCapacity:3];
[dict setObject:test forKey:#"test"];
NSLog(#"%#",dict);
ATTTEst *tester=[dict objectForKey:test];
NSLog(#"%#",tester.obj1.string3);
IT shows null.as output I want to get the value as c for tester.obj1.string3
ATTTEst *tester=[dict objectForKey:test];
should be
ATTTEst *tester=[dict objectForKey:#"test"];
You have used the object test (instead of the string #"test") as key when retrieving the object. I don't think that
was intentionally.
In order to store them into NSDictionary, you don't need to encode them.
Just do:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:, attestObject,#"attestObject", attest2Object,#"atttest2" nil];
Where attestObject and attest2Object are the objects you want to store, and strings are their keys.
This has nothing to do with encoding...

Resources