I have an app which downloads books from LibriVox and I'm using Core Data with MagicalRecord to store informations about them.
I have an object Book which stores book information and it has a list of Chapters which stores information about chapters/files I downloaded.
I download files in background (with AFNetworking) and execute NSNotificationCenter notification on the main thread when the file is downloaded. I subscribe with AppDelegate to that notification and create books and chapters in there and save the context.
On my view controllers I subscribe to NSManagedObjectContextDidSaveNotification for MR_contextForCurrentThread to reload views when new data is available. The problem is that on one view controller the book appears fine but chapters (on second view controller) doesn't.
When my BookViewController gets NSObjectContextDidSaveNotification for the first time I get a book and its chapters (well one since book is created when the first chapter is downloaded). But when I tap the book to get ChaptersViewController to display chapters - it's empty.
[Chapter MR_findByAttribute: @"book" withValue: book andOrderBy: @"order" ascending: YES] returns empty array, MR_findAll equivalent does the same, fetch results controller also does not return any chapters even though when I log it into a console NSLog(@"Book: %@", book); it has a NSSet of chapters inside of it. Even countEntitiesWithPredicate returns 0. I could just use the NSSet from the Book object itself but it doesn't really solves the problem plus I need those objects in order.
[Chapter MR_findByAttribute: @"book" withValue: book andOrderBy: @"order" ascending: YES]
NSLog(@"Book: %@", book);
What's even more strange is that after that initial reload of BookViewController which finds a book and chapters inside of that book, every following reloads only returns books, and [Chapter MR_findByAttribute: @"book" withValue: book andOrderBy: @"order" ascending: YES] returns empty array, just like in ChaptersViewController. That is until I kill my application, restart it and start downloading next chapters. Then both - that book and it's chapters appears without any problems (ChaptersViewController reloads correctly and both - old, previously invisible chapters and new ones appears on the list).
To be sure - when I download first file I check if there's already a book for it (there isn't that time), if not - it creates one (I tired both - MR_createEntity and [Book MR_createInContext: [NSManagedObjectContext MR_contextForCurrentThread]]), create chapter (same method), set book property of that chapter to that new book, add chapter to chapters set (addChaptersObject) and after all that I save the context with [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveNestedContexts]
[Book MR_createInContext: [NSManagedObjectContext MR_contextForCurrentThread]]
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveNestedContexts]
Same problem here, as well as described in Issue #225, #210.
The problem is probably related to the nested context drawbacks talked here: http://wbyoung.tumblr.com/post/27851725562/core-data-growing-pains
Can't wait a fix to be committed. I comment out these lines to prevent saving on rootSavingContext:
NSManagedObjectContext+MagicalSaves.m line 83-86:
// if (self == [[self class] MR_defaultContext])
// [[[self class] MR_rootSavingContext] MR_saveInBackgroundErrorHandler:errorCallback];
The problem is temporally gone, though it's not the real fix...
I have a fix for this, but it needs a bit more vetting before i can commit. On the flip side, works great on ios 6 :/
@blackgold9 commit it please!! We would love it.
Please try out the iOS5NestedContextFix branch for a fix here. It's experimental code that may take care of the problem.