Reputation: 1205
i have a problem with MagicalRecord, NSOperationsQueues and NSURLRequests.
I got a Model that handles the Data fetch from the CoreData via MagicalRecord. Thats working fine, but i want to have a background Process to fetch Data via my API from the web via NSURLRequest. Here is the Main Problem. I have to set the scheduleInRunLoop:forMode
on the NSURLConnection
to [connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
The callback from my Model to the NSOperationQueue is now handled by the main thread and everything is working.
I want to save the fetched ressources to the CoreData via MagicalRecord. I set up my [NSManagedObjectContext MR_contextForCurrentThread]
in the start
method from my NSOperationQueue
to handle the saving in the current thread from the NSOperationQueue.
The Main problem i am facing right now is that i call [self.localContent MR_saveToPersistentStoreWithCompletion:]
with a completion block to wait for CoreData to save it to the PersistentStore.
But at this point the completion block is never called and i cant update my NSOperationQueue to finish.
What exactly i am doing wrong at this point?
If i am saving it in the defaultContext my App crashes randomly with the following statements:
[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke3 Fatal Exception NSGenericException * Collection <__NSCFSet: 0x1d0490c0> was mutated while being enumerated.
NSOperation low-priority concurrency-limiting queue Crashed
-[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] EXC_BAD_ACCESS
I know it's because of accessing and saving the same time on the same context, but i don't figgure it out to fix this.
I am using the newest version of MagicalRecord
Upvotes: 1
Views: 394
Reputation: 69687
From your limited description and lack of actual code, all I can offer is that you are mutating your set from another thread. Most likely, you have more than one NSoperation working with the exact same data set, and also sharing the same managed object context. When one thread saves, the context is updated on one thread, thus marking the collection as modified. Then the other thread comes in and the next access triggers this error. You need to pin down how many operations you're firing at once, make sure all operations have a single context and that you are no sharing one context for all operations.
Upvotes: 0