Re: Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
Re: Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
- Subject: Re: Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
- From: Randall Meadows <email@hidden>
- Date: Wed, 4 Jun 2008 17:46:37 -0600
On Jun 4, 2008, at 5:10 PM, Alexander Hartner wrote:
I have an application which refreshes a NSTable with data from a
network server. The refresh can take several seconds, and might even
fail when the server is not accessible. During the refresh process I
would like to display a sheet with a spinning progress indicator.
Everything worked sort of OK until I added some threading. The
reason I wanted to add a new thread was to free up the main thread
to prevent the spinning beach ball from appearing. The beach ball
appeared every-time when then process took a little bit longer then
it should.
On my main form I have a button which is link to the refresh action:
- (IBAction) refresh:(id)sender
{
[NSThread detachNewThreadSelector:@selector(processRefreshGroup:)
toTarget:self withObject:sender];
//[self processRefreshGroup:sender];
}
- (void) processRefreshGroup:(id)sender
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSApplication * application = [NSApplication sharedApplication];
[application beginSheet:reloadGroupsPanel modalForWindow:
[application keyWindow] modalDelegate:nil didEndSelector:nil
contextInfo:nil];
[application endSheet:reloadGroupsPanel];
@try
{
[reloadGroupsProgressIndicator startAnimation:sender];
[groups removeAllObjects];
//This method can take a long time to complete
[self doExpensiveWorkHere];
[reloadGroupsProgressIndicator stopAnimation:self];
[application stopModal];
[reloadGroupsPanel close];
}
@catch (NSException * exception)
{
[reloadGroupsProgressIndicator stopAnimation:self];
[application stopModal];
[reloadGroupsPanel close];
[errorMessageField setString:[exception reason]];
NSApplication * application = [NSApplication sharedApplication];
[application beginSheet:errorPanel modalForWindow:[application
keyWindow] modalDelegate:nil didEndSelector:nil
contextInfo:nil];
[application endSheet:errorPanel];
}
//Reload data on NSTable
[groupSelectionTable reloadData];
[pool release];
}
I have a couple of questions:
1.) I gathered i have to create a new NSAutoReleasePool in my
"threaded" method. Is this correct ?
Yes.
2.) During execution of this I am updating the UI components from a
thread which is not the main thread.
Ruh-roh!
This produces several error messages. How can I updated UI components.
performSelectorOnMainThread
I tried using performSelectorOnMainThread,
There you go!
but this didn't work either. Not sure if this is the right approach.
Define "didn't work". Maybe show us the code you used; I'm pretty
this is the right way to do it.
3.) Is there an alternative way to achieve this without the beach
ball appearing. I understand what caused it was the fact that I
never exited the action method, at least not quickly. I would prefer
an approach which did not require a new thread to be created.
You're on the right track; you must have had some other problem with
your performSelectorOnMainThread solution.
_______________________________________________
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