Magical Record, cannot save an object: contextDidSave == NO, error = nil

2k Views Asked by At

I'm trying to persist new entities:

- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
    NSMutableArray *mutableArray = [NSMutableArray array];

    [Quiz MR_truncateAll];
    [[NSManagedObjectContext MR_context] MR_saveWithBlock:^(NSManagedObjectContext *localContext) {

        for (NSDictionary *dictionary in quizzess) {
            Quiz *quiz = [Quiz MR_createEntity];
            [quiz fromDictionary:dictionary];
            [mutableArray addObject:quiz];
        }
    } completion:^(BOOL contextDidSave, NSError *error) {
        BlockSafeRun(completion, mutableArray, contextDidSave, error);
    }];
}

Or like this:

- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
    NSMutableArray *mutableArray = [NSMutableArray array];

    [Quiz MR_truncateAll];

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        for (NSDictionary *dictionary in quizzess) {
            Quiz *quiz = [Quiz MR_createEntity];
            [quiz fromDictionary:dictionary];
            [mutableArray addObject:quiz];
        }
    } completion:^(BOOL contextDidSave, NSError *error) {
        BlockSafeRun(completion, mutableArray, contextDidSave, error);
    }];
}

But in completion block I receive contextDidSave == NO, error == nil. So I cannot figure out what went wrong. Are there some obvious mistakes I made? How can I debug that issue?

//////

2015-06-17 20:39:27.061 HITO[6733:618953] Set root saving context: <NSManagedObjectContext: 0x16dbe070>
2015-06-17 20:39:27.062 HITO[6733:618953] Created new main queue context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.063 HITO[6733:618953] Set default context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.316 HITO[6733:618953] [HockeySDK] WARNING: Detecting crashes is NOT enabled due to running the app with a debugger attached.
2015-06-17 20:39:28.829 HITO[6733:618953] Created new private queue context: <NSManagedObjectContext: 0x16d57870>
2015-06-17 20:39:28.831 HITO[6733:619027] Created new private queue context: <NSManagedObjectContext: 0x16ea4ec0>
2015-06-17 20:39:28.841 HITO[6733:619027] NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING

update

Following code from MR:

- (void) MR_saveWithOptions:(MRSaveOptions)saveOptions completion:(MRSaveCompletionHandler)completion;
{
    __block BOOL hasChanges = NO;

    if ([self concurrencyType] == NSConfinementConcurrencyType)
    {
        hasChanges = [self hasChanges];
    }
    else
    {
        [self performBlockAndWait:^{
            hasChanges = [self hasChanges];
        }];
    }

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

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

        return;
    }

So, hasChanges returns NO.

1

There are 1 best solutions below

6
On BEST ANSWER

Your objects have no changes occurring in the save block. There are two problems I see here.

  1. You are creating your new objects in the MR_defaultContext when you need to be creating them in the localContext that is the saving context for your save block.

Try this:

- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
    NSMutableArray *mutableArray = [NSMutableArray array];


    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        for (NSDictionary *dictionary in quizzess) {
            Quiz *quiz = [Quiz MR_createInContext:localContext];
            [quiz fromDictionary:dictionary];
            [mutableArray addObject:quiz];
        }
    } completion:^(BOOL contextDidSave, NSError *error) {
        BlockSafeRun(completion, mutableArray, contextDidSave, error);
    }];
}
  1. Your objects are not actually being deleted before you start importing the new objects. This probably isn't causing your context to have no changes but I'll discuss this as well.

[Quiz MR_truncateAll] just sets all of your Quiz objects deletedproperty to true. This means the next time that the context is saved or processed, the changes will be saved.

So, when you create your new saving context that context still has those objects. I'm not sure what your fromDictionary method is doing, but if its relying on an database, then it's not going to have it.

You need to do it like this:

- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
    NSMutableArray *mutableArray = [NSMutableArray array];


    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        [Quiz MR_truncateAllInContext:localContext];
        [localContext processPendingChanges];

        for (NSDictionary *dictionary in quizzess) {
            Quiz *quiz = [Quiz MR_createInContext:localContext];
            [quiz fromDictionary:dictionary];
            [mutableArray addObject:quiz];
        }
    } completion:^(BOOL contextDidSave, NSError *error) {
        BlockSafeRun(completion, mutableArray, contextDidSave, error);
    }];
}

This way, you are deleting the objects in the saving context. You have to also remember to call processPendingChanges on the saving context to delete the objects from the context, rather than just marking them to be deleted.