Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: CoreData and insert in multithread



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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.