FROM : m
DATE : Sun Oct 17 00:50:47 2004
On Oct 16, 2004, at 12:40 PM, John C. Daub wrote:
> Now I have refactored my application a bit so that while the work is
> going
> on I display a progress sheet with progress bar and Stop button. To do
> this,
> I had to put my work into a thread. But from what I was reading about
> threads, since I'm going to need to have the thread collect my data
> then
> pass the data back to the main thread, I need to use something like
> distributed objects to pass that data back.
[snip]
> My guess is because the data object I'm
> actually given isn't the real data object but instead an NSProxy
> version of
> the object...
I think your guess is right on.
You don't need to use DO, though it is sometimes more convenient. You
just need to ensure that your threads aren't accessing your data object
at "at the same time".
Here's a strategy: have your main thread allocate the data object and
pass it to your worker thread (without retaining a reference to it),
then you can just have the worker thread hand it back when it's done
(perhaps by calling
performSelectorOnMainThread:withObject:waitUntilDone: on some suitable
selctor).
something like this (typed in mail, not tested, obviously)
- (void) kickOffLongOperation
{
[NSThread detachNewThreadSelector:@(longOperation:) toTarget:self
withObject:[[MyDataThing alloc]init]];
}
- (void) hereIsTheResult:(MyDataThing*)dataThing
{
// pop the data thing on the undo stack
....
}
- (void) longOperation:(MyDataThing*) dataObject
{
...
while (stillComputing)
{
[dataObject addData:moreData];
}
// all done, transfer object to main thread
[objectInMainThread
performSelectorOnMainThread:@selector(hereIsTheResult:)withObject:
dataObject waitUntilDone:NO];
...
}
_murat
DATE : Sun Oct 17 00:50:47 2004
On Oct 16, 2004, at 12:40 PM, John C. Daub wrote:
> Now I have refactored my application a bit so that while the work is
> going
> on I display a progress sheet with progress bar and Stop button. To do
> this,
> I had to put my work into a thread. But from what I was reading about
> threads, since I'm going to need to have the thread collect my data
> then
> pass the data back to the main thread, I need to use something like
> distributed objects to pass that data back.
[snip]
> My guess is because the data object I'm
> actually given isn't the real data object but instead an NSProxy
> version of
> the object...
I think your guess is right on.
You don't need to use DO, though it is sometimes more convenient. You
just need to ensure that your threads aren't accessing your data object
at "at the same time".
Here's a strategy: have your main thread allocate the data object and
pass it to your worker thread (without retaining a reference to it),
then you can just have the worker thread hand it back when it's done
(perhaps by calling
performSelectorOnMainThread:withObject:waitUntilDone: on some suitable
selctor).
something like this (typed in mail, not tested, obviously)
- (void) kickOffLongOperation
{
[NSThread detachNewThreadSelector:@(longOperation:) toTarget:self
withObject:[[MyDataThing alloc]init]];
}
- (void) hereIsTheResult:(MyDataThing*)dataThing
{
// pop the data thing on the undo stack
....
}
- (void) longOperation:(MyDataThing*) dataObject
{
...
while (stillComputing)
{
[dataObject addData:moreData];
}
// all done, transfer object to main thread
[objectInMainThread
performSelectorOnMainThread:@selector(hereIsTheResult:)withObject:
dataObject waitUntilDone:NO];
...
}
_murat
| Related mails | Author | Date |
|---|---|---|
| John C. Daub | Oct 16, 21:40 | |
| m | Oct 17, 00:50 | |
| John C. Daub | Oct 17, 03:24 | |
| m | Oct 17, 03:47 | |
| John C. Daub | Oct 17, 04:43 |






Cocoa mail archive

