-
-
Save 0thernet/0d85e45d7a5fd1a0094b to your computer and use it in GitHub Desktop.
Revisions
-
tonyarnold revised this gist
Feb 2, 2013 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -32,12 +32,13 @@ NSManagedObjectSubclass *myObject = [NSManagedObjectSubclass MR_findFirst]; [MagicalRecord saveWithBlockAndWait:^(BOOL success, NSError *error) { // Make your changes here. // This block will be called on the main thread. You can safely access objects from outside the block here: [myObject MR_deleteEntity]; // This method will not return until the save has completed }]; // Do post processing here — maybe update the UI? ``` -
tonyarnold revised this gist
Feb 2, 2013 . 1 changed file with 22 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,6 @@ Assuming that you don't care which NSManagedObjectContext is used, and you just want to make some changes and save them in the background, use the following method. 90% of the time, this is what you'll want. ```objc NSManagedObjectSubclass *myObject = [NSManagedObjectSubclass MR_findFirst]; [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { @@ -15,9 +14,30 @@ NSManagedObjectSubclass *myObject = [NSManagedObjectSubclass MR_findFirst]; NSManagedObjectSubclass *myLocalObject = [myObject MR_inContext:localContext]; // It's now safe to make changes to `myLocalObject` } completion:^(BOOL success, NSError *error) { // Generally, you'll use this completion block to update your UI, or handle success/errors // This block is always called on the main thread }]; ``` # Standard foreground save If you need to save some changes, but need the save operation to block the current thread until it has finished writing to disk — use the following method: ```objc NSManagedObjectSubclass *myObject = [NSManagedObjectSubclass MR_findFirst]; [MagicalRecord saveWithBlockAndWait:^(BOOL success, NSError *error) { // Make your changes here. // This block will be called on an unspecified thread — it may not be the main thread, so continue to access objects as if it were a background thread: NSManagedObjectSubclass *myLocalObject = [myObject MR_inContext:localContext]; // It's now safe to make changes to `myLocalObject` // This method will not return until the save has completed }]; ``` -
tonyarnold revised this gist
Feb 2, 2013 . 2 changed files with 23 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # Common Save Patterns # Standard background save Assuming that you don't care which NSManagedObjectContext is used, and you just want to make some changes and save them in the background, use the following method. 90% of the time, this is what you'll want. ```objc NSManagedObjectSubclass *myObject = [NSManagedObjectSubclass MR_findFirst]; [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { // Make changes to your managed objects here, create new objects, or delete existing objects // There's no need to call save within this block // This block is called on an unspecified background thread NSManagedObjectSubclass *myLocalObject = [myObject MR_inContext:localContext]; // It's now safe to make changes to `myLocalObject`. } completion:^(BOOL success, NSError *error) { // Generally, you'll use this completion block to update your UI, or handle success/errors // This block is always called on the main thread }]; ``` -
tonyarnold created this gist
Feb 1, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # Common Save Patterns TBD