Re: Open a panel in secondary thread?
Re: Open a panel in secondary thread?
- Subject: Re: Open a panel in secondary thread?
- From: Rob Petrovec via Cocoa-dev <email@hidden>
- Date: Sun, 22 Mar 2020 14:38:07 -0600
I would not do this. It is widely documented that AppKit API that produce UI
elements, like NSOpenPanel, are _not_ thread safe. So you may find your app
hitting some memory stomping issues or strange crashes/exceptions due to this.
Specifically what problems you will hit are anyones guess, but its just a
matter of time. There is a reason Xcode has the Main Thread Checker to catch
UI elements being used on secondary threads.
Lets come at this from a different direction: What are you trying to do? Why
do you think you need to use the open panel on a secondary thread?
—Rob
> On Mar 22, 2020, at 1:11 PM, Gabriel Zachmann via Cocoa-dev
> <email@hidden> wrote:
>
>>
>> Don't know if this helps you but you can look into dispatch_sync
>> and dispatch_async.
>>
>
> Thanks a lot!
> That made things very easy.
>
> I am opening the panel now with this piece of code:
>
> __block NSURL * user_permitted_url;
> __block long int result;
> // UI stuff must be executed in the main thread
> dispatch_sync( dispatch_get_main_queue(), ^(void)
> {
> result = [self askUserToOpenDirectory: resolvedurl
> newURL: & user_permitted_url];
> }
> );
>
> (where my method askUserToOpenDirectory opens the NSOpenPanel and gets the
> URL.)
>
> BTW:
> just in case others stumble over this:
> in my case, I am opening the NSOpenPanel with the directory set to
> resolvedurl, for which I am asking permission. So the user is just supposed
> to click "OK".
> So, I thought, I don't need to do anything else;
> but it turns out I get permission from the OS really only if I actually get
> the URL using
> oPanel.URL
> , even if it is the same as resolvedurl!
>
>
> Best regards, Gabriel
>
>
> _______________________________________________
>
> 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
_______________________________________________
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