ios - 在获取异步数据时,在获取所需结果之前提前调用通知 block 。想要在获取所有数据后重新加载 collectionView

标签 ios swift uicollectionview grand-central-dispatch dispatch-async

在 viewDidLoad() 中调用了以下方法并使用 Firestore 缓存获取数据然后附加到数组。获得完整响应后,我想重新加载我的 collectionView(我正在通过通知 block 执行此操作)。请让我知道我哪里错了,这是我的代码:

  func getArchivedTasks(completion: @escaping ([ArchivedTasksModel],Bool) -> ()) {

    var archTasks : [ArchivedTasksModel] = []
    let viewModels = self.fetchTaskCardsFromDB()
    let group = DispatchGroup()

    for newItem in viewModels {

        let newTask = ArchivedTasksModel()
        newTask.taskCard = newItem

        firestoreManager.getLastComment(taskId: newTask.taskCard?.id ?? "") { (commnt, res) in
            group.enter()
            newTask.comment = commnt
            archTasks.append(newTask)
            group.leave()
        }
    }
    group.notify(queue: .main) {
        completion(archTasks, true)
    }
}

最佳答案

调用异步任务之前必须进入

group.enter()
firestoreManager.getLastComment(taskId: newTask.taskCard?.id ?? "") { (commnt, res) in ...

关于ios - 在获取异步数据时,在获取所需结果之前提前调用通知 block 。想要在获取所有数据后重新加载 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59244566/

相关文章:

ios - Swift 2 IOS 9 = clang : error: linker command failed with exit code 1 (use -v to see invocation)

swift - 函数声明中的下划线有什么作用?

ios - UICollectionView 显示与所选单元格相关的图像

ios - 从另一个 View Controller 更新 UILabel

ios - 问 : UITests - How to get collectionCell element inside collectionView nested with TableView cell?

ios - 从应用程序使用 iPhone USB 网络共享连接

ios - PFQueryTableViewController 不加载图像 parse.com

ios - 给表情符号着色 "image"

ios - 底部 5 行的 UITableView 背景颜色

ios - 如何根据 SWIFT 中的内容调整 TextView 的高度?