Search code examples
nsmanagedobjectcontext

nsmanageobjectcontext.save stopped working in iOS7


The project I inherited was built and launched in April 2013, and it worked perfectly for ios 5.1, ios 6.0 and ios 6.1.

However i just installed the app to an iphone that has ios7.0 and it didn't work. I looked at the code and I see that the app downloads some JSON data from the web, and then when it tries to run the following 3 lines of code, "nothing happens" after the 2nd line.

NSError error = nil;
BOOL isSuccessful =[self.tempMoc save:&error];  // where tempMoc is a NSManagedObjectContext
NSLog(@"errrrrrr ----- %@ --- errrrrrr", error);

When I say nothing happens, I mean that the code execution stops on the 2nd line, and no code after that line gets fired. I tried putting a breakpoint on the second line, then stepping into the function, but nothing happens...xcode doesn't show me anything new afterwards. The app in my simulator also hangs.

I tried changing the deployment target of my project from ios6 to ios7. Again, this yielded no effects.

What should I do next?

ADDITIONAL NOTES

I've been reading other stack overflow answers and some people say the a hanging [NSManagedObjectContext save] might be a threading issue. I'm not sure how to confirm if my issue is a threading issue. I know that there's only ONE place that calls the [NSManagedObjectContext save], and that's the one place where things are hanging. I tried putting a

[self.tempMoc.persistentStoreCoordinator lock];

right after instantiating self.tempMoc, but that had no effect.


Solution

  • I figured out the issue.

    It was indeed multiple threads manipulating the NSManagedObjectContext that caused hte save function to hang.

    My solution was to rewrite the code to get rid of all the extra threads. I was left with only the main thread and this fixed the issue.