Skip to content

Instantly share code, notes, and snippets.

@ChrisDrit
Created November 24, 2010 07:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDrit/713297 to your computer and use it in GitHub Desktop.
Save ChrisDrit/713297 to your computer and use it in GitHub Desktop.
insertNewObject
- (void)insertNewObject {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
// Loop through array of hashes and save
for (NSMutableDictionary *story in stories) {
NSManagedObject *storiesData = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[storiesData setValue:[story objectForKey: @"title"] forKey:@"title"];
[storiesData setValue:[story objectForKey: @"summary"] forKey:@"summary"];
[storiesData setValue:[story objectForKey: @"link"] forKey:@"link"];
[storiesData setValue:[story objectForKey: @"date"] forKey:@"date"];
[storiesData setValue:[NSDate date] forKey:@"timeStamp"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
// empty the stories array
stories = nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment