gpt4 book ai didi

ios - 在继续循环之前让后台任务完成

转载 作者:行者123 更新时间:2023-11-28 08:48:01 24 4
gpt4 key购买 nike

我正在尝试在重复循环内运行后台任务(query.findobjectinbackground 是准确的)。问题是我需要在继续并再次运行循环之前完成此操作。

我需要它来完成,因为在后台任务中正在填充一个数组,这最终会导致填充一个 UITable。

当我在当前状态下运行它时,循环会运行并结束,而后台任务仍在运行。

这是我的代码:

//array Im trying to populate
var contentArray:NSMutableArray = NSMutableArray()

func queryStatusTable() {
contentArray.removeAllObjects()

//query my table
let queryUser = PFUser.query()
let objectIdString = PFUser.currentUser()?.objectId
queryUser?.whereKey("objectId", equalTo: objectIdString!)
queryUser?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if let objects = objects {
for object in objects {

//used for the repeat loop
//object["userFriendsArray"] is an array I got from my above query

var i:Int = 0
let count:Int = object["userFriendsArray"].count

repeat {

//second query
let queryFriendStatus = PFQuery(className: "Content")
queryFriendStatus.whereKey("userObjectId", equalTo: object["userFriendsArray"][i])

//this is what I want to finish before the loop moves on
queryFriendStatus.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if let objects = objects {
for object in objects {
self.contentArray.addObject(object["content"])
}
}

})

//so i dont want this to run until the above task finishes
//and putting this inside the above task doesnt work because it will never run if its inside the findobjectinbackground
i++

} while (i < count)
}
}

//this is where I reload the table data to populate it with
//whats in my *contentArray*
self.firstTableView.reloadData()
})
}

那么我如何确保在 self.firstTableView.reloadData() 运行之前填充 contentArray

任何帮助将不胜感激,谢谢!

最佳答案

为什么不在建立数据时定期更新表格?

它会给用户一些东西看,而且会出现得更快。

我之前通过每 50 行(或其他)调用重新加载然后在最后再次调用它来获取最后一行来完成此操作

请记住从后台数据收集中通过主队列进行调用 - 就像这样

dispatch_async(dispatch_get_main_queue()) {
self.firstTableView.reloadData()
}

关于ios - 在继续循环之前让后台任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com