views:

87

answers:

2

Hi,

I tried to save data and merge with CoreData and multi-thread for iPhone app. But I can't get managed objects in the main thread after merging.

I wrote code just like this:

[managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                                       withObject:notification
                                    waitUntilDone:YES];

[self performSelectorOnMainThread:@selector(didMerged:) withObject:objectIds waitUntilDone:YES];

So I tried to pass objectIds to get NSManagedObject instances in the main thread which were generated in another thread. At first I tried "objectWithId" method but it generated fault objects. Then I tried "existingObjectWithID" method but it generated objects partly and others were nil with following Error:

[Error] Error Domain=NSCocoaErrorDomain Code=133000 "Operation could not be completed. (Cocoa error 133000.)"

What is wrong? Is there any way how to retrieve all objects by objectIds after merging in another thread?

Thank you.

A: 

It seems your context merge failed.

developer documentation on error 133000

NSManagedObjectReferentialIntegrityError = 133000

NSManagedObjectReferentialIntegrityError Error code to denote an attempt to fire a fault pointing to an object that does not exist. The store is accessible, but the object corresponding to the fault cannot be found. Available in Mac OS X v10.4 and later. Declared in CoreDataErrors.h.

Martin Brugger
Thank you for your fast reply.Hmm... In another thread, I can save data without any error. How can I merge them to the context in the main thread well without #133000 error?
Emmettoc
A: 

First, you need to unroll your errors. Change the output to:

NSLog(@"Error: %@\n%@", [error localizedDescription], [error userInfo]);

That will give you a lot more information.

Second, if you are working with a single context in multiple threads you are doing it wrong. You need to review the documentation on Core Data and threading. The basic rule is: One Context Per Thread; Period. If you need to manage data across multiple threads look into watching the save notifications from the background threads on the main thread. I would suggest reviewing my articles on the Mac Developer Network for examples of this.

Marcus S. Zarra
Thank you. I'll check the error in detail. Yes, I created contexts for each thread.
Emmettoc