这取决于你的意思。鉴于您的示例代码,您似乎希望您的函数在嵌套块完成时调用一个块。所以你想要这样的东西:
+ (void)doMyThingWithBlock:(void(^)(void))block
{
FBRequest *request = [FBRequest requestForGraphPath:kFacebookQueryMeKey];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// do whatever you want with the connection ...
if (block) block();
}];
}
另一方面,也许您希望外部函数在返回之前等待嵌套块完成。在这种情况下,它有点复杂。您需要使用锁来等待块完成,如下所示:
#define kMyThingNotDone 0
#define kMyThingDone 1
+ (void)doMyThingAndWait
{
NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:kMyThingNotDone];
FBRequest *request = [FBRequest requestForGraphPath:kFacebookQueryMeKey];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// do whatever you want with the connection ...
[lock lock];
[lock unlockWithCondition:kMyThingDone];
}];
[lock lockWhenCondition:kMyThingDone];
[lock unlock];
}
请注意 - 您不想在主线程上执行此操作,否则您将锁定用户界面。您还必须确保请求不会等待您从中调用它的线程上发生任何事情,否则您将最终陷入死锁。