Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get objects after CoreData Context merged

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.

like image 254
Emmettoc Avatar asked Jun 05 '10 13:06

Emmettoc


People also ask

When managed object context saves its changes where does it push changes next?

When one of the managed object contexts is saved, its changes are pushed through the persistent store coordinator to the persistent store.

Can't reassign an object to a different store once it has been saved?

Cause: The object you are trying to assign to a store has already been assigned and saved to a different store. Remedy: To move an object from one store to another, you must create a new instance, copy the information from the old object, save it to the appropriate store, and then delete the old instance.

What is context Core Data?

From your perspective, the context is the central object in the Core Data stack. It's the object you use to create and fetch managed objects, and to manage undo and redo operations. Within a given context, there is at most one managed object to represent any given record in a persistent store.

Is Core Data a Threadsafe?

Core Data is designed to work in a multithreaded environment. However, not every object under the Core Data framework is thread safe. To use Core Data in a multithreaded environment, ensure that: Managed object contexts are bound to the thread (queue) that they are associated with upon initialization.


2 Answers

There are two types of object ids. Before you save a NSManagedObject it has temporary object id. After saving, it will get its final id. So you may use the wrong id...

Read Managed Object IDs and URIs here: https://developer.apple.com/documentation/coredata/nsmanagedobjectid

like image 197
Daniel Avatar answered Sep 30 '22 14:09

Daniel


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.

like image 30
Martin Brugger Avatar answered Sep 30 '22 14:09

Martin Brugger