Created
August 10, 2016 18:26
-
-
Save JoelJWest/a5fb5bcc360a8914c49e7b342f2c0c1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| - (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