re: CoreData and insert in multithread
re: CoreData and insert in multithread
- Subject: re: CoreData and insert in multithread
- From: Ben Trumbull <email@hidden>
- Date: Wed, 30 Jan 2008 16:49:12 -0800
malcom,
There's no easy way to get Core Data to see unsaved data in another
context (on another thread).
For non-inserted objects, you can replicate the changes in the other
thread by passing a dictionary between the threads. Some methods
that can help do this:
On NSManagedObject:
- (NSDictionary *)committedValuesForKeys:(NSArray *)keys;
- (NSDictionary *)changedValues;
In KVC protocol generally:
-(NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
For inserted objects, you're SOL.
In general, though, it's better to just not have the problem. If the
background thread makes a bunch of changes, save the batch, and pull
(fetch/objectWithID:) them onto the main thread.
Most apps don't require synchronously forcing the main thread to
update status from background threads in the middle of a task.
Accepting a little bit of staleness in the UI, and having it refresh
every few seconds, is usually acceptable.
If it's crucial, you can use proxies on the main thread, backed
either by a managed object (for saved changes), or an NSDictionary
(for unsaved changes).
--
-Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden