0

I am calling the function archiveQueue to store object of type MYJSON at various times through out the program, and now I want to restore everything that I stored uptill now in an array.

Following is the function that I am using to store the objects :

- (void)archiveQueue:(MYJSON *)msg{

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:msg];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *file = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myqueue.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:file contents:data attributes:nil];
    NSLog(@"ArchiveQueue: file = %@", file);

}

Following is the function I am using to restore the objects.

- (void)restoreQueue {


    NSLog(@"RestoreQueue: Restoring queue from Document Directory.");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *file = [[paths objectAtIndex:0]  stringByAppendingPathComponent: @"myqueue.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSData *data = [fileManager contentsAtPath:file];


        // How to unarchive that array of objects being stored. 

}

I want to know how should I unarchive everything I stored as an array of objects ?

Nilesh Agrawal
  • 3,002
  • 10
  • 26
  • 54
  • If you are dealing with json why not just save as json? – Brad Allred May 07 '14 at 22:32
  • Due to some requirement of project I am not allowed to save as json. – Nilesh Agrawal May 07 '14 at 22:34
  • I'm confused. Is `jsonObject` not the data you stored after unarchiving into it? – Chuck May 07 '14 at 22:39
  • Okie Chuck ! Sorry I deleted that line now.I just want to restore all the objects I store through NSKeyArchiver. – Nilesh Agrawal May 07 '14 at 22:44
  • You're going to get out of NSKeyedUnarchiver exactly what you put in to NSKeyedArchiver. What did you put in? – MJN May 07 '14 at 23:22
  • @NileshAgrawal: So, what is the problem? You should be getting back the object tree rooted in the MYJSON object you stored. Are you not getting that back? What is going wrong? – Chuck May 07 '14 at 23:22
  • Try setting some breakpoints and confirm what type of object is being unarchived. It seems like you think the root object will be an NSDictionary. But the object you save is a MYJSON. – MJN May 07 '14 at 23:24
  • I am calling the function archieveQueue multiple times and storing some object of the type MYJSON. and calling restoreQueue only once to get all those objects. – Nilesh Agrawal May 07 '14 at 23:28
  • 2
    `createFileAtPath:contents:attributes:` will overwrite everything that was at that location previously. You'll always end up with one object. – MJN May 07 '14 at 23:33
  • Okie So what changes in code should be made so that I can store so many objects and restore them in array. – Nilesh Agrawal May 07 '14 at 23:39

1 Answers1

1

In order to save a custom class with NSKeyedArchiver, that class needs to implement the NSCoding protocol. Even if instances of your class are included as members of an NSArray or NSDictionary you need to have them conform.

Using createFileAtPath:contents:attributes: will overwrite the contents of that file.

Here are some options:

Archive an array of objects every time.

get an array of all objects
add your new object
write that array to disk

Archive objects to unique files in a directory

documents/mySpecialObjects/A.myObject
documents/mySpecialObjects/B.myObject
documents/mySpecialObjects/C.myObject
...

create local NSMutableArray
enumerate all files inside of documents/mySpecialObjects
unarchive each file and add to your array
MJN
  • 10,748
  • 1
  • 23
  • 32