【问题标题】:iOS Box API - wait for request to finishiOS Box API - 等待请求完成
【发布时间】: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


【解决方案1】:

(Swift 5.x) 你可以使用这个代码:

var a: [String:Any]

func myFunction(completion:@escaping (Bool) -> () ) {

    DispatchQueue.main.async {
        // For example your action on a
    }
}


myFunction { (status) in
    if status {
        print(a!)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    相关资源
    最近更新 更多