Search code examples
ios7nsmanagedobjectcontext

NSManagedObjectContext save fails when saving too many items in ios7?


I have the following two lines of code that works almost all the time:

NSError *error = nil;
BOOL isSuccessful =[self.tempMoc save:&error];   // tempMoc is a NSManagedObjectContext

This code works as expected on ios6 simulator, ios6 physical devices and ios7 simulator. The variable isSuccessful evaluates to Yes.

However, on ios7 physical devices, isSuccessful evaluates to NO. Why is that?

error is always nil in all four cases mentioned.

Does anyone know why this is the case and how I can get isSuccessful to evaluate to YES on ios7 physical devices?

ADDITIONAL DETAILS

After more debugging I noticed something . Prior to the tempMoc save above, I have this code running:

- (void)saveCompatibilities:(NSArray *)objects {
    NSString *entityName = NSStringFromClass([Compatibility class]);

        for (NSDictionary *newObjectDict in objects) {
            Compatibility *object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.tempMoc];
            object.prod1 = newObjectDict[@"prod1"]; // value is just the letter a
            object.prod2 = newObjectDict[@"prod2"]; // value is just the letter a

        }


    }

I noticed that if the number of iterations in the for loop is very large, like let's say 50 000 loops, then I encounter the ios7 isSuccessful == NO issue mentioned above. If it is only say 20 loops, then isSuccessful evaluates to yes. The number of loops that causes the ios7 isSuccessful failure is different with every single run.

I'm starting to think this is a memory issue with my device?


Solution

  • Sounds like a memory issue. Try saving periodically, or on memory pressure. You could also make a child context to save on a different thread.