Reputation: 11
Occasionally I have to do a popup alert window in my Cocoa code segments. Previously I used NSAlert
directly then have runModal
to go while I found that the NSRunAlertPanel
is more easier to achieve my goal. So I decided to switch all my alert functions to NSRunAlertPanel
. It seemed okay at most time。
Now I'm adding multithreading. I found that NSRunAlertPanel
appears clearly slower than NSAlert
when calling back in the main thread.
Code segments:
Firstly I create a thread:
[NSThread detachNewThreadSelector: @selector(tryRunLoop:) toTarget:self withObject:nil];
Then this functiontryRunLoop
in this thread call the alert window function in the main thread:
while(1)
[self performSelectorOnMainThread:@selector(showAlert:) withObject:anObject waitUntilDone:YES];
The function showAlert
in main thread do the rest things:
NSRunAlertPanel(@"Warning:",@"Just testing", @"YES", nil, nil);
As time goes by the response of the popup window appears slower and slower.If I use NSAlert
instead of NSRunAlertPanel
, or did not run the popup method in main thread, the symptom should disappear.
I also found that the CPU usage was also different between these two methods. Obviously NSAlert
costs low CPU usage while hitting the button all the time.
Is someone able to explain these phenomenons?
PS: I was not allowed to put the whole original project online so that I've created a simple Cocoa project in Github to simulate the symptom and the URL ,please take a look at the Known issues
in Readme file at first.
Upvotes: 0
Views: 404
Reputation: 15013
Alright, the short answer is don't use NSRunAlertPanel
. That family of functions have been discouraged for some time now, and superseded by NSAlert
. Use NSAlert
instead.
(Unfortunately the class reference for NSRunAlertPanel
etc. doesn't mention this; I'm trying to remember where it was first documented; perhaps a release note)
Upvotes: 0