Skip to content

Instantly share code, notes, and snippets.

@matzew

matzew/Fetch Secret

Last active December 10, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matzew/7772301c7e87e2ad6648 to your computer and use it in GitHub Desktop.
Save matzew/7772301c7e87e2ad6648 to your computer and use it in GitHub Desktop.
CoreData Fetch, via AGCoreData (remote persistence)
...
// 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];
...
// 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