使用 firebase 数据库和存储下载数据时索引超出范围异常

Index out of Range exception when using firebase database and storage to download data

这是我从数据库中检索用户数组和 post 对象的方法。

func getRecentPost(start timestamp: Int? = nil, limit: UInt, completionHandler: @escaping ([(Post, UserObject)]) -> Void) {

    var feedQuery = REF_POSTS.queryOrdered(byChild: "timestamp")
    if let latestPostTimestamp = timestamp, latestPostTimestamp > 0 {
        feedQuery = feedQuery.queryStarting(atValue: latestPostTimestamp + 1, childKey: "timestamp").queryLimited(toLast: limit)
    } else {
        feedQuery = feedQuery.queryLimited(toLast: limit)
    }

    // Call Firebase API to retrieve the latest records
    feedQuery.observeSingleEvent(of: .value, with: { (snapshot) in
        let items = snapshot.children.allObjects

        let myGroup = DispatchGroup()

        var results: [(post: Post, user: UserObject)] = []

        for (index, item) in (items as! [DataSnapshot]).enumerated() {
            myGroup.enter()
            Api.Post.observePost(withId: item.key, completion: { (post) in
                Api.User.observeUser(withId: post.uid!, completion: { (user) in
                    results.insert((post, user), at: index) //here is where I get my error -> Array index is out of range
                    myGroup.leave()
                })
            })
        }
        myGroup.notify(queue: .main) {
            results.sort(by: {[=10=].0.timestamp! > .0.timestamp! })
            completionHandler(results)
        }
    })

}

这是从我的视图控制器调用的方法。我目前正在使用纹理 UI 来帮助更快更平滑 UI.

var firstFetch = true
func fetchNewBatchWithContext(_ context: ASBatchContext?) {
    if firstFetch {
        firstFetch = false
        isLoadingPost = true
        print("Begin First Fetch")
        Api.Post.getRecentPost(start: posts.first?.timestamp, limit: 8  ) { (results) in
            if results.count > 0 {
                results.forEach({ (result) in
                    posts.append(result.0)
                    users.append(result.1)
                })
            }
            self.addRowsIntoTableNode(newPhotoCount: results.count)
            print("First Batch Fetched")
            context?.completeBatchFetching(true)
            isLoadingPost = false
            print("First Batch", isLoadingPost)
        }

    } else {
        guard !isLoadingPost else {
            context?.completeBatchFetching(true)
            return
        }
        isLoadingPost = true

        guard let lastPostTimestamp = posts.last?.timestamp else {
            isLoadingPost = false
            return
        }
        Api.Post.getOldPost(start: lastPostTimestamp, limit: 9) { (results) in
            if results.count == 0 {
                return
            }
            for result in results {
                posts.append(result.0)
                users.append(result.1)
            }
            self.addRowsIntoTableNode(newPhotoCount: results.count)
            context?.completeBatchFetching(true)
            isLoadingPost = false
            print("Next Batch", isLoadingPost)

        }
    }


}

在第一段代码中,我调试了看是否能弄清楚发生了什么。目前,firebase 正在返回正确数量的对象,我已将查询限制为 (8)。但是,在我突出显示错误发生的地方,索引在即将插入第五个对象时跳转,index[3] -> 第 4 个对象在数组中,到 index[7]-> 第 5 个对象即将被解析和解析第 5 个对象时插入。

因此,它不会从索引[3] 转到索引[4],而是跳转到索引[7]。 有人可以帮助我了解发生了什么以及如何解决它吗?

for 循环在其线程上继续执行,而 observeUserobservePost 回调在其他线程上执行。查看您的代码,您可能只需使用 appending 对象到 results 数组而不是 inserting。这是有道理的,因为无论如何你都是在 for 循环之后排序,那么为什么顺序很重要?