Skip to content

Instantly share code, notes, and snippets.

@0thernet
Forked from tonyarnold/mr-save-patterns.md
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save 0thernet/0d85e45d7a5fd1a0094b to your computer and use it in GitHub Desktop.

Select an option

Save 0thernet/0d85e45d7a5fd1a0094b to your computer and use it in GitHub Desktop.

Revisions

  1. @tonyarnold tonyarnold revised this gist Feb 2, 2013. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions mr-save-patterns.md
    Original 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 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];
    // This block will be called on the main thread. You can safely access objects from outside the block here:
    [myObject MR_deleteEntity];
    // It's now safe to make changes to `myLocalObject`
    // This method will not return until the save has completed
    }];
    // Do post processing here — maybe update the UI?
    ```
  2. @tonyarnold tonyarnold revised this gist Feb 2, 2013. 1 changed file with 22 additions and 2 deletions.
    24 changes: 22 additions & 2 deletions mr-save-patterns.md
    Original 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`.
    // 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
    }];
    ```
  3. @tonyarnold tonyarnold revised this gist Feb 2, 2013. 2 changed files with 23 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    # Common Save Patterns

    TBD
    23 changes: 23 additions & 0 deletions mr-save-patterns.md
    Original 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
    }];
    ```
  4. @tonyarnold tonyarnold created this gist Feb 1, 2013.
    3 changes: 3 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Common Save Patterns

    TBD