swift - 如何使用 Swift 检查 Parse.com 中的后台查询是否已完成

标签 swift parse-platform

我需要用从 Parse.com 检索到的不同用户数据填充一个数组。使用 getFirstObject 显然不是一个好主意,但我不知道如何找出何时为每个用户获取所有数据:

    myObjects = []
    for user in users { // users is an array of PFUsers
        let query = PFQuery(className: "MyClass")
        query.whereKey("asdh74tbf", equalTo: user)

        query.getFirstObjectInBackgroundWithBlock{ (object: PFObject!, error: NSError!) -> Void in
            if let object = object {
                self.myObjects.append(object)
            } else {
                // handle error
            }
        }
    }

我想在检索到所有对象后执行self.tableView.reloadData()。如何做到这一点?

最佳答案

使用调度组。检查here在“等待排队任务组”中了解更多信息。

您还提到您想要获取每个用户的所有对象。因此,您可以使用 findObjectsInBackgroundWithBlock 方法来代替 getFirstObject 。该 block 返回解析后的响应。因此,将其与调度组结合起来,您的代码可能看起来像这样

//create a new dispatch group
let group: dispatch_group_t = dispatch_group_create();
var myObjects = []
for user in users { // users is an array of PFUsers
    let query = PFQuery(className: "MyClass")
    query.whereKey("asdh74tbf", equalTo: user)

    //enter the dispatch group
    dispatch_group_enter(group);

    query.findObjectsInBackgroundWithBlock{ (objects: NSArray!, error: NSError!) -> Void in
        if let object = object {
            self.myObjects.append(object)
        } else {
            // handle error
        }

        //leave the dispatch group
        dispatch_group_leave(group);
    }
}

//will be called when all the async operations entered in the group are finished.  
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    self.tableView.reloadData()
    });

关于swift - 如何使用 Swift 检查 Parse.com 中的后台查询是否已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34744781/

相关文章:

ios - 使用 AVAudioPlayer 和有效的 URL 播放声音

ios - 如何关闭 iOS 设备

ios - 如何通过在 swift playground 中传递 Int 数组来创建求和函数并调用它?

ios - 错误 : Value of type '[String]' has no member 'objectAtIndex' while implementing UISearchController

ios - 使用未解析的标识符错误

ios - 将来自 parse.com 的数据保存在字符串中的问题

javascript - 解析.com : cannot create more than 9 objects using cloud job

ios - 从解析中查询图像和文本

Swift 获取今天之前五年的日期

Swift map 引脚标注和参数