Re: Open a panel in secondary thread?
Re: Open a panel in secondary thread?
- Subject: Re: Open a panel in secondary thread?
- From: Sandor Szatmari via Cocoa-dev <email@hidden>
- Date: Mon, 23 Mar 2020 10:57:01 -0400
> On Mar 23, 2020, at 10:01 AM, Gabriel Zachmann <email@hidden> wrote:
>
>
>>>> I would not do this. It is widely documented that AppKit API that produce
>>>> UI elements, like NSOpenPanel, are _not_ thread safe.
>>>
>>> Right, and that is why I have to execute any UI code in the main thread,
>>> or so I thought.
>>> As far as I understand, the dispatch_get_main_queue is executed by the main
>>> thread, isn't it?
>> Yes
>
> So, why is
> dispatch_sync( dispatch_get_main_queue(), block opening a NSOpenPanel )
> a bad idea?
I think it’s fine if the following is true… you are ok with blocking your
secondary thread while the user interacts with the panel.
>
>>>
>>> When the user selects a directory, I collect all files in that sub-tree
>>> (aka "scan").
>>> That might involve aliases. which means I need to ask the user to open the
>>> directory that alias points to.
>>> Because that scan can take several seconds (10-20),
>>> I wanted to do that scan in a secondary thread so that it can update
>>> progress info in the UI.
>>
>> This is all reasonable… I guess there may be two options, ask the user on
>> the main thread, before you are on your background thread,
>
> Would be doable, but would complicate the code, because
>
> I would first need to scan the whole directory tree for aliases,
I haven’t had much experience with needing to request permission to access
files, but I thought you’d have to ask permission before scanning. Are you
allowed to scan before you ask for permission?
> get user permission ,
> then pass the array of directories to a secondary thread.
>
> Also, I would like to give the user progress info while the secondary thread
> is collecting.
Yes, understood. Does the user have to ask for this process to begin, or does
it start automatically? Is the user allowed to interact with the app while
this is going on, or does the UI just update with progress?
Sandor
>
>> and only start the background thread after the user answers, or dispatch the
>> UI work to the main thread from your background thread as your code sample
>> showed.
>
> So my solution is viable ?
>
>
> 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