Cocoa使用[ALAssetsLibrary enumerateGroupsWithTypes:]时的线程同步
我最近和一些人一样,发现[ALAssetsLibrary enumerateGroupsWithTypes]喜欢在另一个线程上运行它的块。遗憾的是,Apple 没有记录这一点:-)
在我当前的情况下,我需要等待枚举完成,然后主线程返回任何结果。我显然需要某种线程同步。
我读过有关 NSLock 和 NSLock 的内容。 NSConditionLock,但似乎还没有任何东西符合“向阻塞线程发出该工作线程已完成的信号”的要求。这似乎是一个足够简单的需求 - 谁能指出我正确的方向?
你的线索&嘘声,一如既往地受到欢迎,
M.
I have recently, like a few people, discovered that [ALAssetsLibrary enumerateGroupsWithTypes] likes to run its blocks on another thread. What a shame that Apple didn't document that :-)
In my current circumstance I need to wait for the enumeration to complete, before the main thread returns any results. I clearly need some sort of thread synchronisation.
I've read about NSLock & NSConditionLock, but nothing yet seems to fit the requirement of 'signal a blocked thread that this worker thread has completed'. It seems like a simple enough need - can anyone point me in the right direction?
Your clue & boos, are most welcome as always,
M.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该框架不会在单独的线程上运行这些块。它只是将它们作为同一运行循环中的附加事件运行。为了证明这一点,请尝试这个
我的输出是
意味着该块正在主线程中被调用。这只是一个单独的事件。
要解决您的问题,您只需在到达最后一步时以某种方式从块内返回您的值即可。您可以看出这是最后一步,因为您的块将以 nil 调用组对象。
编辑:例如使用此块
The framework doesn't run these blocks on a separate thread. It just runs them as additional events in the same run-loop. To prove it, try this
My output from this was
Meaning that the block was being called in the main thread. It's just a separate event.
To solve your problem, you just need to return your value somehow from within the block when you reach the last step. You can tell it's the last step because your block will be called with nil for the group object.
EDIT: for instance use this block
答案是这样使用 NSConditionLock 类......
然后分离你的线程,或者在我的例子中调用 [ALAssetsLibrary enumerateGroupsWithTypes:]。然后用这个阻塞父线程...
当所有工作在子/工作线程中完成时,用这个取消阻塞父线程...
The answer is to use the NSConditionLock class thusly ...
Then spin off your thread, or in my case a call to [ALAssetsLibrary enumerateGroupsWithTypes:]. Then block the parent thread with this ...
When all work is done in the child/worker thread, unblock the parent with this ...
只需使用这个:
Simply use this: