Skip to content

Instantly share code, notes, and snippets.

@ejknapp
Created December 1, 2009 00:40
Show Gist options
  • Save ejknapp/245941 to your computer and use it in GitHub Desktop.
Save ejknapp/245941 to your computer and use it in GitHub Desktop.
//Add a message to Core Data
- (void)addMessage:(NSDictionary *)newMessageDictionary {
NSDictionary *messageDictionary = [newMessageDictionary objectForKey:@"message"];
// NSLog(@"In addMessage");
// NSLog(@"new message dictionary:\n%@", newMessageDictionary);
NSEntityDescription *entity =
[self.messageFetchedResultsController.fetchRequest entity];
Message *message =
[NSEntityDescription insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:self.managedObjectContext];
// The message id returned by the Flak server
NSNumber *messageId = [messageDictionary objectForKey:@"id"];
self.currentMaxMessageId = messageId;
//[message setValue:messageId forKey:@"messageId"];
message.messageId = messageId;
// The user id returned by the Flak server
[message setValue:[messageDictionary objectForKey:@"user_id"] forKey:@"userId"];
// The kind of message
[message setValue:[messageDictionary objectForKey:@"kind"] forKey:@"kind"];
// The body of the message, if it's not a string then set and empty string
NSString *body = [messageDictionary objectForKey:@"body"];
if (![body isKindOfClass:[NSString class]]) {
body = @"";
}
[message setValue:body forKey:@"body"];
// The first name returned by the Flak server
[message setValue:[messageDictionary objectForKey:@"user_first_name"] forKey:@"firstName"];
// The last name returned by the Flak server
[message setValue:[messageDictionary objectForKey:@"user_last_name"] forKey:@"lastName"];
[message setValue:[self createLocalDate: messageDictionary forKey:@"created_at"] forKey:@"createdAt"];
//Testing the dateFormatter
// NSString *dateString = [messageDictionary objectForKey:@"created_at"];
// NSLog(@"date from formatter:\n%@\n%@", dateString, [self.dateFormatter dateFromString:dateString]);
// The last updated date returned by the Flak server
// sourceDate = [self.dateFormatter dateFromString:[messageDictionary objectForKey:@"updated_at"]];
//
// sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
// destinationTimeZone = [NSTimeZone systemTimeZone];
//
// sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
// destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
// interval = destinationGMTOffset - sourceGMTOffset;
//
// destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
[message setValue:[self createLocalDate: messageDictionary forKey:@"updated_at"] forKey:@"updatedAt"];
// [message setValue:[self.dateFormatter dateFromString:
// [messageDictionary objectForKey:@"updated_at"]] forKey:@"updatedAt"];
// NSLog(@"new message object:\n%@", message);
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"There was an error saving a new message: %@", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment