Magic entry cannot save object: contextDidSave == NO, error = nil

I am trying to save new objects:

- (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 the completion block I get contextDidSave == NO, error == nil. So I can't figure out what went wrong. Are there some glaring mistakes I've made? How can I debug this problem?

//////

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

The following code is 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.

+3


source to share


1 answer


Your objects have no changes going on in the save block. I see two problems here.

  • You create your new objects in MR_defaultContext

    when you need to create them in localContext

    , which is the save context for the save block.

Try the following:

- (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 deleted before you start importing new objects. This probably doesn't result in your context having no changes, but I'll discuss that as well.


[Quiz MR_truncateAll]

simply sets all object Quiz

objects to a deleted

value true

. This means that the next time the context is saved or processed, the changes will be saved.

So, when you create your new save context, that context still has these objects. I'm not sure what your method does fromDictionary

, but if it relies on the database then it won't 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 delete objects in the save context. You also have to remember to call processPendingChanges

into the save context to remove objects from the context, not just mark them for deletion.

+6


source







All Articles