Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

playerObject is an NSDictionary from JSONObjectWithData:options:error:

I'm trying to update a CoreData entity. My code looks like this:

if ([results count] > 0)
{
    newPlayer = (Player *)[results objectAtIndex:0];
} else {
    //Add a new player
    newPlayer = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:temporaryContext];
}
//Assume these exist and are not null
newPlayer.email = [playerObject objectForKey:@"email"];
newPlayer.username = [playerObject objectForKey:@"username"];
newPlayer.continent = [playerObject objectForKey:@"continent"];

//Dates
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
newPlayer.created = [df dateFromString: [playerObject objectForKey:@"created"]];

//Optional properties
if ([[playerObject objectForKey:@"title"] isKindOfClass:[NSNull class]])
{
    newPlayer.title = nil;
} else {
    newPlayer.title = [[playerObject objectForKey:@"title"] stringValue];
}

Is there any way to make it less verbose and have I left myself open to any issues?

share|improve this question
Take a look at JSRestNetworkKit and stop parsing the JSON yourself :) github.com/JaviSoto/JSRestNetworkKit – Javier Soto Sep 5 '12 at 15:53
Looks interesting but I'd just want to use the mapping from a dictionary to CoreData entity. Would this be possible? – Daniel Skinner Sep 17 '12 at 9:10
You mean without making requests to an API? Yes, absolutely, you would just have to make your CoreData entities inherit from JSBaseCoreDataBackedEntity and use + (id)updateOrInsertIntoManagedObjectContext:(NSManagedObjectContext *)managedObjectContext withDictionary:(NSDictionary *)dictionary; – Javier Soto Sep 17 '12 at 18:37
Excellent. I'll take a look into that then, since I'm using JSON-RPC for the web service. – Daniel Skinner Sep 18 '12 at 11:08

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.