-
-
Save matzew/7772301c7e87e2ad6648 to your computer and use it in GitHub Desktop.
CoreData Fetch, via AGCoreData (remote persistence)
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
... | |
// create the CoreData util/helper (or plugin...) | |
AGCoreDataHelper *helper = [[AGCoreDataHelper alloc] initWithConfig:^(id<AGCoreDataConfig> config) { | |
// apply some config options | |
[config setManagedObjectModel:managedObjectModel]; | |
[config setBaseURL:baseURL]; | |
[config setAuthMod:myMod]; | |
}]; | |
// get the IMPORTANT managedObjectContext, from the plugin: | |
NSManagedObjectContext *context = helper.managedObjectContext; | |
// vanilla,standard CoreData work, to execute a fetch request..... | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tag" inManagedObjectContext:context]; | |
[fetchRequest setEntity:entity]; | |
// add a notification, which is invoked, once the FETCH has been completed | |
[[NSNotificationCenter defaultCenter] addObserverForName:@"AFIncrementalStoreContextDidFetchRemoteValues" object:context queue:[NSOperationQueue mainQueue] | |
usingBlock:^(NSNotification *note) { | |
NSDictionary *userInfo = [note userInfo]; | |
NSArray *fetchedObjects = [userInfo objectForKey:@"AFIncrementalStoreFetchedObjectsKey"]; | |
// work with the received TAG objects: | |
}]; | |
// perform the actual fetch (which translates into a HTTP GET against the AG backend) | |
NSError *error = nil; | |
[context executeFetchRequest:fetchRequest error:&error]; |
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
... | |
// create the CoreData util/helper (or plugin...) | |
AGCoreDataHelper *helper = [[AGCoreDataHelper alloc] initWithConfig:^(id<AGCoreDataConfig> config) { | |
// apply some config options | |
[config setManagedObjectModel:managedObjectModel]; | |
[config setBaseURL:baseURL]; | |
[config setAuthMod:myMod]; | |
}]; | |
// get the IMPORTANT managedObjectContext, from the plugin: | |
NSManagedObjectContext *context = helper.managedObjectContext; | |
Tag *tag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:context]; | |
tag.title = @"My Tag"; | |
... | |
// Save everything (causes a HTTP POST, against the AG backend) | |
NSError *error = nil; | |
if ([context save:&error]) { | |
NSLog(@"The save was successful!"); | |
_finishedFlag =YES; | |
} else { | |
NSLog(@"The save wasn't successful: %@", [error userInfo]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment