iOS: conditions for success is NO and error is nil for [MagicalRecord saveWithBlock:completion:]

StackOverflow https://stackoverflow.com/questions/23397815

  •  13-07-2023
  •  | 
  •  

Question

What are the conditions where [MagicalRecord saveWithBlock:completion:] will show a success of NO in the completion block and have an error of nil?

One condition I have seen is where a fetch inside the save block returns nil and hence there was no save to do. But I am seeing this where the fetch in the save block returns a valid managed object.

Example code:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    TransactionQueueItem *item = [self queueItemForToken:token context:localContext];
    if (nil == item)
       NSLog(@"item is nil for token %@", token);

    item.lastTryDate = [NSDate date];
    if (nil != errorMsg)
       item.lastTryError = errorMsg; 
    }
  completion:^(BOOL success, NSError *error) {
    if (!success)
       {
         NSLog(@"Error %@ updating last Try time item for token %@", error, token);
         }
       else
       {
         if (nil != completionBlock)
            completionBlock();
         }
    }];

I am seeing that the item comes back ok but then on the save I get success = NO and error = nil and I am not seeing what is happening.

Various Google/Bing searches did not point to conclusive results. Thanks.

Was it helpful?

Solution

I was running into the same misunderstanding but it seems that success just means that it didn't save the data, not that an error occured. In case of which there are no data to save, it won't return success but your data is still there.

This is the log output I'm getting after having success to NO and error to nil:

-08-01 14:07:01.003 TEST[39216:f03] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dbac50) → Saving <NSManagedObjectContext (0x8dbac50): *** UNNAMED ***> on *** BACKGROUND THREAD ***
2014-08-01 14:07:01.003 TEST[39216:f03] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dbac50) → Save Parents? 1
2014-08-01 14:07:01.004 TEST[39216:f03] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8dbac50) → Save Synchronously? 0
2014-08-01 14:07:01.004 TEST[39216:f03] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8db99c0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING

And in the NSManagedObjectContext+MagicalSave.m:41 file, you see that it checks for changes in the current context but doesn't save it if there's no new data:

if (![self hasChanges]) {
    MRLog(@"NO CHANGES IN ** %@ ** CONTEXT - NOT SAVING", [self MR_workingName]);

    if (completion)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(NO, nil);
        });
    }

    return;
}

OTHER TIPS

The success parameter is the result of the call to save: on the localContext. That is, MagicalRecord does some basic error handling and other logging for you, and wraps the save: method. We return that value for you in this callback to give you an opportunity to handle any problems yourself, in addition to what MagicalRecord already does for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top