Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
- Subject: Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
- From: Alexander Hartner <email@hidden>
- Date: Thu, 5 Jun 2008 00:10:32 +0100
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 ?
2.) During execution of this I am updating the UI components from a
thread which is not the main thread. This produces several error
messages. How can I updated UI components. I tried using
performSelectorOnMainThread, but this didn't work either. Not sure if
this is the right approach.
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.
Thanks for you help
Alex
_______________________________________________
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