Created
February 19, 2011 11:50
-
-
Save xslim/835019 to your computer and use it in GitHub Desktop.
Example how I use CoreData & Networking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
AppDelegate.m | |
*/ | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
[[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ | |
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:note]; | |
}]; | |
DataController *adapter = [DataController sharedDataController]; | |
adapter.persistentStoreCoordinator = self.persistentStoreCoordinator; | |
adapter.managedObjectContext = self.managedObjectContext; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DataController.m | |
*/ | |
- (void)refresh { | |
//dataAdapter = NetworkController, loads & returns parsed JSON | |
[self.dataAdapter refreshWithCompletionHandler:^(NSArray *items, NSError *error) { | |
NSLog(@"Loaded items: %@", items); | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
if (!items || [items isKindOfClass:[NSNull class]] || [items count] < 1) return; | |
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; | |
[moc setPersistentStoreCoordinator:self.persistentStoreCoordinator]; | |
NSEntityDescription *albumEntity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:moc]; | |
и тд | |
NSError *error; | |
if (![moc save:&error]) { | |
NSLog(@"Error saving DB: %@, %@", [error localizedDescription], [error userInfo]); | |
} | |
[moc release]; | |
}); | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ViewController.m (subclass from UITableViewControler) | |
Using Table & NSFetchedResultController | |
delegate=self | |
*/ | |
- (IBAction)refresh { | |
DataController *adapter = [DataController sharedDataController]; | |
[adapter refresh]; | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
//[self.itemsTableView endUpdates]; | |
//NSLog(@"loaded"); | |
[self removeLoadingView]; | |
[self.itemsTableView reloadData]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment