Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSKeyedArchiver to store custom data model

I'm working on a rss reader. It is just a tableview and each cell shows a custom data model RSSEntry. And I have a NSMutableArray allEntries which contains all RSSEntry I got from the server. This is what RSSEntry looks like:

    @interface RSSEntry : NSObject <NSCoding> {
        NSString *_blogTitle;
        NSString *_articleTitle;
        NSString *_articleUrl;
        NSDate *_articleDate;
    }

I want to restore data from local archive so when I quit and open this app again, the tableview is populated with data when last refresh.

I've read example in 'Beginning iOS5 Programming', but it only store and restore only one data model. So I don't know how to store this mutable array full of my custom data model and restore it.

I use following code to store and restore data:

// STORE
NSMutableData *data = [[NSMutableData alloc] init]; 
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:entry forKey:kDataKey];
[archiver finishEncoding];

// RESTORE
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath:kFilename]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
RSSEntry *entries = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];

I can only store and restore one data model in this way.

Who can give me a hand. I'm new to iOS programming, maybe it's a little dull .sorry.

like image 966
wyp Avatar asked Jun 07 '12 09:06

wyp


3 Answers

Place all the entries you want to store into an NSArray and use this code:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:entries];

Where entries is your array of entries. Nice and easy, just one line of code.

To unarchive, just use

NSArray *entries = [NSKeyedUnarchiver unarchiveObjectWithData:data];

NSKeyedArchiver is able to archive any data structure, even a very complex one, of Objective-C objects that implement the NSCoding protocol, including nested NSArrays, NSDictionarys, and custom objects, to a file. NSKeyedUnarchiver does the reverse, taking any data produced by NSKeyedArchiver and reestablishing the original object graph that was archived. Technically, they could just be a single class, but Apple decided to separate them by functionality because this is more grammatically correct or something :)

I assume you are able to write the resulting NSData object to a file, and read it back again.

like image 74
Greg Avatar answered Nov 09 '22 00:11

Greg


You could store an NSDictionary as following

NSDictionary *Your_NSDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"Obj1", @"Key1",
                               @"Obj2", @"Key2", nil];
//store dictionary
NSMutableData *yourData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:yourData];
[archiver encodeObject:Your_NSDictionary forKey: @"key"];
archiver finishEncoding];
[yourData writeToFile:@"FilePath" atomically:YES];
[yourData release];
[archiver release];


//Load dictionary

NSData *yourData = [[NSMutableData alloc]initWithContentsOfFile:@"FilePath"];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:yourData];
Your_NSDictionary = [unarchiver decodeObjectForKey: @"key"];
[unarchiver finishDecoding];
[unarchiver release];
[yourData release];
like image 20
Omar Abdelhafith Avatar answered Nov 09 '22 01:11

Omar Abdelhafith


For iOS 12:

for archivedDataWithRootObject:

NSData *myDicData = [NSKeyedArchiver archivedDataWithRootObject:AllDataDic 
                                     requiringSecureCoding:YES error:nil];
[[NSUserDefaults standardUserDefaults] setObject:myDicData forKey:@"Yourkey"];

for unarchivedObjectOfClass:

NSData *tempData = [[NSUserDefaults standardUserDefaults] valueForKey:@"Yourkey"];
NSDictionary *myDicProfile = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSDictionary class] 
                                                fromData:tempData error:nil];
like image 41
Abhishek Mishra Avatar answered Nov 09 '22 02:11

Abhishek Mishra