سؤال

I get JSON from a web service which I need to save locally using Core Data. This is part of a sync operation which is performed after certain interval. I need to first convert JSON to NSManagedObject and check if it is already saved locally then just update existing otherwise insert new NSManagedObject.

An NSManagedObject being checked, whether it is saved already or not. can also have relations with other NSManagedObjects (which also need to be part of predicate)

Can anyone suggest any considerable lib to handle this deserialization/serialization from/to JSON <-> NSManagedObject.

هل كانت مفيدة؟

المحلول

i use RestKit for this purposes, it's pretty easy to use

نصائح أخرى

@Eugen

RestKit seems complicated. I had to parse pretty complicated JSON and save in Core data. Also the data on server can change and in next parsing the local Core data values needs to be updated not inserted new.

But on the web service call, I need to authenticate by setting HTTP header user=access_token, password=mypassword.

There is only one method in RestKit to requesting and mapping directly to managedObject and only that method is not working properly while sending the request. I get 403 response. All the other methods which are not related to NSManagedObject authenticates and gets good JSON in the response.

I have wasted so much time trying to make RESTKIT work and now I feel I should try some simple way. Can anyone suggest any good lib or any other suggestions.

Thank you.

Concerning RestKit: I prefer doing web service calls as described here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

As to your deserialisation problem: You might want to convert the NSData object retrieved by the web service to a JSON structure like this:


NSMutableDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];

Then you can use https://gist.github.com/pkclsoft/4958148 for populating a NSManagedObject with like this:


Entity *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:[self managedObjectContext]];
[entity populateFromDictionary:dict];

If the json structure and the structure of the NSManagedObject differ, you can manipulate the NSMutableDictionary before using it for populating the NSManagedOject.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top