Skip to content

Instantly share code, notes, and snippets.

@harveyslash
Created August 8, 2016 19:09
Show Gist options
  • Save harveyslash/47d8096e17fe54e51cfae984b412028c to your computer and use it in GitHub Desktop.
Save harveyslash/47d8096e17fe54e51cfae984b412028c to your computer and use it in GitHub Desktop.
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"THIS IS IT %@", [json valueForKey:@"message_create"]);
id jsonBody = [json valueForKey:@"message_create"];
if ([Chat fetchById:[jsonBody valueForKey:@"id"] withContext:self.managedObjectContext]==nil) {
NSLog(@"GOT NIL FROM SOCKET");
Chat *chat = [Chat initWithContext:self.managedObjectContext];
chat.id = [jsonBody valueForKey:@"id"];
chat.body = [jsonBody valueForKey:@"body"];
chat.user = [User fetchByID:[jsonBody valueForKey:@"user_id"] inContext:self.managedObjectContext];
NSString *date = [jsonBody valueForKey:@"created_at"];
chat.created_at = [TimeFormatter NSDateFromServerString:date];
chat.group = [Group fetchByID:[jsonBody valueForKey:@"grp_id"] inContext:self.managedObjectContext];
[self.managedObjectContext save:nil];
}
NSLog(@"Saved");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment