Skip to content

Instantly share code, notes, and snippets.

@JoelJWest
Created August 10, 2016 18:26
Show Gist options
  • Save JoelJWest/a5fb5bcc360a8914c49e7b342f2c0c1d to your computer and use it in GitHub Desktop.
Save JoelJWest/a5fb5bcc360a8914c49e7b342f2c0c1d to your computer and use it in GitHub Desktop.
- (IBAction)remove:(id)sender {
if (self.rdcAccountsFRC.fetchedObjects.count < 1) {
return;
}
BNModelController *const modelController = [BNModelController new];
// BGLManagedObjectContextHolder has helpful methods, particularly related to saving changes to the context
// (i.e., persisting data to disk).
BGLManagedObjectContextHolder *const mainContextHolder = modelController.stack.mainContextHolder;
// This makes modifications to the main thread NSManagedObjectContext in a synchronous way and saves them to disk.
[mainContextHolder performWithAutoSaveInContextAndWait:^ (NSManagedObjectContext *context) {
// Using the find-or-create pattern to make BGLManagedCard instances with unique identifiers.
BGLManagedAccount *account = self.rdcAccountsFRC.fetchedObjects[0];
[context deleteObject:account];
}];
}
- (IBAction)add:(id)sender {
BNModelController *const modelController = [BNModelController new];
// Anything can be created using the find-or-create pattern of helper methods in BGLManagedObjectRepository(Enhance).
BGLManagedObjectRepository *const repository = modelController.repository;
// BGLManagedObjectContextHolder has helpful methods, particularly related to saving changes to the context
// (i.e., persisting data to disk).
BGLManagedObjectContextHolder *const mainContextHolder = modelController.stack.mainContextHolder;
BGLManagedUser *const activeUser = [modelController activeUser];
// This makes modifications to the main thread NSManagedObjectContext in a synchronous way and saves them to disk.
[mainContextHolder performWithAutoSaveInContextAndWait:^ (NSManagedObjectContext *context) {
// Using the find-or-create pattern to make BGLManagedCard instances with unique identifiers.
NSInteger randomID = arc4random() %100;
BGLManagedAccount *account = [repository findOrCreateAccountWithID:[NSString stringWithFormat:@"account%li", (long)randomID] inContext:context];
[account setValue:@"Fake Account" forKey:@"name"];
[account setValue:@"1234" forKey:@"numbers"];
[account setValue:activeUser.primaryInstitution forKey:@"institution"];
[account setValue:activeUser forKey:@"user"];
[account setValue:@(YES) forKey:@"canEnrollInRDC"];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment