【发布时间】:2016-12-10 20:07:17
【问题描述】:
有没有办法等待 Box API 完成所有请求?例如,如果我提出一个文件夹项请求,我想让我的程序等待完成处理程序完成后再继续。
举个例子:
BOXContentClient *contentClient = [BOXContentClient defaultClient];
BOXFolderItemsRequest *listAllInRoot = [contentClient folderItemsRequestWithID:BOXAPIFolderIDRoot];
[listAllInRoot performRequestWithCompletion:^(NSArray *items, NSError *error) {
//Do something with the results here
}
// Wait here for the completion handler to finish before moving on
我尝试过使用 NSCondition,但是我想知道是否有更好的方法。
【问题讨论】:
-
使用 dispatch_group 和 Dispatch_group_wait
-
@Paulw11 - 好的,所以我已经尝试过了,但奇怪的是,它似乎不起作用。我从未见过“Here1”或“Here2”。
BOXFolderItemsRequest *listAllInRoot = [contentClient folderItemsRequestWithID:BOXAPIFolderIDRoot]; dispatch_group_t boxGroup = dispatch_group_create(); dispatch_group_enter(boxGroup); [listAllInRoot performRequestWithCompletion:^(NSArray *items, NSError *error) { std::cout << "Here1\n"; dispatch_group_leave(boxGroup); }]; dispatch_group_wait(boxGroup, DISPATCH_TIME_FOREVER); std::cout << "Here2\n"; -
不要使用 std::cout。使用
NSLog
标签: objective-c box-api