views:

480

answers:

1

Hi everyone

I got a question ;)

Actually I want to use core-data in different threads. I have two UIviews that display data from the same table in the database.

If a remove an entry in the first view I'm supposed save and then ask the PersistentStoreCoordinator to give me (the UIview) a brain new managed object. That what they say in the official video "working with core data" on itunesU

With the great messaging system from cocoa I can deal with a basic synchronization. ( I send a 'has to get new MO' message )

But how am I supposed to properly save and 'close' the DB and then get another managed object.

<### update ###>

I have 2 viewController, one for each of the views I'm deallin with.

If I set up a coredata stack on each of the ViewControllers the data is reachable from each view but no sync.

But if I put a core data stack in the AppDelegate I got an Exception :

*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x498f600 <x-coredata:...>'

what should I do ?

<### update ###>

Thanks in advance for any help on this ;)

+2  A: 

Each view is NOT on a separate thread. The entire UI for your application is on ONE thread.

You do not need to use multi-threading in this situation.

To create a new object you just need to request one using the class method on the NSEntityDescription object. To delete an object you need to use the -deleteObject: method on the NSManagedObjectContext.

update

You should only have one Core Data stack unless you are dealing in a multithreaded environment (your not). Ideally the stack should be created in the Application Delegate and then propagated down to the UIViewController instances. If you are getting an error then you need to solve that error seperately. From the looks of the error you posted, you are retaining/hanging onto a NSManagedObject that has been deleted by Core Data and thus cannot be fulfilled because it is no longer in the store. This is a separate issue from where the stack lives.

On a side note, it is customary to accept an answer to your question and then post a separate question to be answered.

Marcus S. Zarra
Ok thanks for the answer ;)By the way, sorry for the UIview threading mistake, I'm new to cocoa.
adrian Coyle
Thanks for the update, and I'll remember your tip for the forum
adrian Coyle