Ele*_*web 22 iphone grand-central-dispatch ios objective-c-blocks
是否可以在主线程上运行完成块?
例如,我有一个返回值的方法:
- (int)test
{
/* here one method is called with completion block with return type void */
[obj somemethodwithcompeltionblock:
{
/* here I am getting my Int which I want to return */
}
];
}
Run Code Online (Sandbox Code Playgroud)
但我无法看到如何从完成块中返回整数值作为此方法的结果,因为完成块在后台线程上运行.
我怎样才能做到这一点?
Jos*_*erg 29
您缺少一些关于使用块进行异步开发的基础知识.除了自己的范围之外,您不能从任何地方返回调度块.将每个块视为自己的方法,而不是内联代码.
我认为你所寻找的东西与此类似......
- (void)testWithHandler:(void(^)(int result))handler
{
[obj somemethodwithcompeltionblock:^{
int someInt = 10;
dispatch_async(dispatch_get_main_queue(), ^{
handler(10);
});
}
];
}
- (void)callSite
{
[self testWithHandler:^(int testResult){
NSLog(@"Result was %d", testResult);
}];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14649 次 |
最近记录: |