Re: passing complex objects between threads
Re: passing complex objects between threads
- Subject: Re: passing complex objects between threads
- From: Daniel Child <email@hidden>
- Date: Tue, 28 Apr 2009 15:48:11 -0400
Thanks for the sugestion. I will keep the idea of passing object IDs
in mind when that situation comes up. But I think I misled you.
My data was not in Core Data yet, but rather in an archive I needed to
unfreeze and place in Core Data. In the end, I did something similar
to what you suggested—opening a new context in a new thread, parsing
the file from within the thread, and importing and storing in Core
Data. Meanwhile, I updated the original context (app's context) so
that the interface would get updated as data came in. The approach
(option A in my original question) worked fine. But as I said, this
approach skirts the issue.
How do you pass complex objects from one thread to another? OR, to put
it another way, Why do deeper elements disappear? For example, I tried
passing a reference like this:
[openImportThread: self] (where self = appController)
Since I cached the table to be imported as an ivar, that should allow
me to access the table in appController. Unfortunately, the table's
records all disappear and are no longer accessible from the new
thread, even if I go something like:
<WITHIN THREAD>
NSArray *records = [[owner myTable] records];
// records are no longer there.... even though other single-level
table info is (numRecs etc.)
The same thing happens if I try to cache the table in a global
variable. The minute I enter the thread, access to the actual records
(found within an array instance variable) disappears.
<WITHIN THREAD>
NSArray *records = [globalTableRef records]; // records are no longer
there....
// records are no longer there.... even though other single-level
table info is (numRecs etc.)
In the case of global variables, that could be a deep-shallow copy
distinction, but in the case of accessing the appController's cached
ivar, I don't see how copying is even involved.
Why do the records disappear (from access) once you enter another
thread? And how do you prevent the objects inside a collection-based
ivar from disappearing when you enter a new thread?
On Apr 25, 2009, at 1:53 AM, Chris Hanson wrote:
Indeed. Furthermore, since you're working with Core Data, you may
not actually want those 50K objects passed from one thread to
another; Core Data strongly prefers threads to work in entirely
separate managed object contexts. If you use one
NSManagedObjectContext per thread, but these contexts use the same
NSPersistentStoreCoordinator, Core Data has just the solution for you.
I did something similar to this.
If you do want to still pass information from the background thread
to the main thread, all you need to pass are the managed object IDs
of the (saved) objects the main thread will be interested in. You
can just use -[NSObject
performSelectorOnMainThread:withObject:waitUntilDone:] for this and
pass either individual object IDs or collections of them. Then on
the main thread, you can ask the main thread's managed object
context for the managed object with that ID, and it'll hand you back
an appropriate one that you can use for your user interface.
Doesn't work because the data is not in Core Data yet. I will keep in
mind for future, though. Thanks._______________________________________________
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