1

I use SBJsonParser for parsing a large json response I query from a server. I'd like to use a timestamp approach to know when I actually have to re-obtain and re-parse all the data again, so I'd like to know how I can save my data for those times I don't need to request the large JSON package to refresh my data.

I'm thinking I should be using the NSCoding protocol and archiving roughly like the following:

// alloc nsdata obj
NSMutableData *data = [[[NSMutableData alloc] init] autorelease];

// create archiver
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];  

// archive whiteboard
[archiver encodeObject:mSaveData forKey:@"Data"];
[archiver finishEncoding];
[data writeToFile:dataPath atomically:YES];

but is there a simple way to encode this JSON object that is really an NSArray of NSDictionaries, or do I have to meticulously encode each field within this object?

Maybe there is a simpler way altogether, like saving the json object itself as a string (before it is parsed with the objectWithData method) and re-parsing it each time the app loads and wants to use the local copy?

2 Answers 2

2

Since the data you want to save are just NSArrays, NSDictionaries, and presumably things like NSStrings and NSNumbers, I don't see why you need to "meticulously encode each field". The way you did it with NSKeyedArchiver is valid, and you don't need any further code to save. Some other options:

  • Use +[NSKeyedArchiver archiveRootObject:toFile:].
  • Use SBJSON to turn the data back into JSON and store the JSON string as UTF-8 data.
  • Save it out as a plist using +[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:].
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but my understanding was that the object being encoded would have to implement the NSCoding protocol and then essentially call encode___ methods (in the encodeWithCoder: method) for each saved piece of data. Are you saying I can simply use the encodeObject:forKey: method and pass in the NSArray, or alternatively pass the NSArray as the root object in the archiveRootObject:toFile: method (without going over each field) and it will just save all the data even when it is a nested array of dictionaries and arrays etc?
Pass the NSArray as the root object, and you should be good to go. When you decode it, you get back the array as the root object.
ok. this is because the NSArray object natively implements the nscoding protocol as needed?
Yes; any object that adopts the NSCoding protocol (as you can tell from the API reference) can be encoding using NSKeyedEncoder. The only time you have to implement initWithCoder: and encodeWithCoder: is if you want to be able to encode one of your own classes using NSKeyedEncoder.
0

AsiHTTP allows you to cache your requests. It implements the features that you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.