GCD具有嵌套的解析查询 [英] GCD with nested Parse Queries
问题描述
func getPosts(skip: Int){
var query = PFQuery(className: self.parseClassName!)
query.includeKey("posted_by")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
if let objects = objects as? [PFObject] {
var requestGroup = dispatch_group_create()
for post in objects
{
dispatch_group_enter(requestGroup)
let queryKommentar1 = PFQuery(className:"Comment")
queryKommentar1.whereKey("posted_to", equalTo: post)
queryKommentar1.limit = 3
queryKommentar1.includeKey("written_by")
queryKommentar1.findObjectsInBackgroundWithBlock() {
(commentObjects: [AnyObject]?, error: NSError?) -> Void in
//Creating UITableViewCells from data and store in array
dispatch_group_leave(requestGroup)
}
}
println("Successfully retrieved \(objects.count) posts.")
dispatch_group_notify(requestGroup, dispatch_get_main_queue()) {
println("All done")
}
}
}
}
}
因此,我不确定是否误解了调度组,但我的意图是针对不同的类进行两个解析查询,并根据提供的数据创建TableViewCells.这可以正常工作,但是由于我不希望在用户滚动表时加载数据,因此我想预加载数据并创建单元格,然后将它们存储在Array中.由于我想删除任何活动指示并重新加载表,因此在获取完成之前,我(在Som Googleing之后)认为调度组可能是一个很好的解决方案.但是,所有完成"永远不会在控制台中打印.
So, I'm not sure if I misunderstood dispatch groups, but my intention is to make two Parse Queries, targeting different classes, and create TableViewCells from the data provided. This works fine, but since I don't want the data to load when the user is scrolling the table, I want to preload the data and create the cells, and store them in an Array. Since I would like to remove any Activity Indication, and reload the table, by the time the fetch is complete I though (after som Googleing) that dispatch groups might be a good solution for this. However, "All done" is never printed in the console.
当我围绕外部查询创建一个调度组(在查询之前进入该组,并在代码块的最后一行保留)时,它运行良好.我究竟做错了什么?嵌套异步调用时不可能使用此功能吗?
When I made a dispatch group around the outer query (entering the group just before the query, and leaving as the last line in the block) that worked fine. What am I doing wrong? Is it impossible to use this when nesting asynchronous calls?
仅供参考,我删除了很多代码,例如创建单元格并使用了Parse中的数据,因为我想避免您阅读这些混乱的信息.
FYI, I removed a lot of code, like creating the cells and using the data from Parse, since I would like to spare you from reading that mess.
推荐答案
此调度组模式基本上是正确的.
This dispatch group pattern is basically right.
我建议在dispatch_group_enter
和dispatch_group_leave
处记录一些消息,看看它是否按照您认为的方式被调用,并且每个enter
都被一个leave
偏移.
I would suggest logging some message at dispatch_group_enter
and at dispatch_group_leave
and see if it's getting called as you think it should and that every enter
is offset by a leave
.
如果dispatch_group_leave
的出现次数少于对dispatch_group_enter
的调用次数,则不会调用dispatch_group_notify
块.
If the number of occurrences of dispatch_group_leave
are less than the number of calls to dispatch_group_enter
, the dispatch_group_notify
block will not get called.
也许在内部findObjectsInBackgroundWithBlock
闭包中有一些路径阻止了它触发dispatch_group_leave
调用.
Perhaps you have some path in that inner findObjectsInBackgroundWithBlock
closure that is preventing it from hitting the dispatch_group_leave
call.
这篇关于GCD具有嵌套的解析查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!