Objective C - 块问题?
我有以下方法
+ (NSString*)getMeMyString
{
NSString *result;
dispatch_async(dispatch_get_main_queue(), ^{
result = [ClassNotThreadSafe getString];
});
return result;
}
如何使块同步完成其工作,以便在检索结果之前不会返回结果?
I have the following method
+ (NSString*)getMeMyString
{
NSString *result;
dispatch_async(dispatch_get_main_queue(), ^{
result = [ClassNotThreadSafe getString];
});
return result;
}
How can i make the block to do it's job synchronously, so that it doesn't return the result before it was retreived?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在调用
dispatch_async
来异步调度您的块。如果您的目标是阻止主线程,请尝试使用dispatch_sync
或dispatch_main
。Grand Central Dispatch 参考
You are calling
dispatch_async
which dispatches your block asynchronously. Try usingdispatch_sync
ordispatch_main
if your goal is to block the main thread.Grand Central Dispatch Reference
使用dispatch_sync而不是dispatch_async - 那么当前线程将被阻塞,直到该块在主线程上完成执行。
Use
dispatch_sync
instead ofdispatch_async
- then the current thread will be blocked until the block has finished executing on the main thread.既然您似乎想在不同的线程上执行一个方法并获取返回值,为什么不使用 NSInitation 呢?
Since it seems like you want to perform a method on a different thread and get a return value, why don't you use an NSInvocation?