gpt4 book ai didi

当 Firebase 查询限制超过 9 时,iOS 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 11:19:37 33 4
gpt4 key购买 nike

正如标题所示,我调用 Firebase 数据库,查询返回限制为 9,并且应用程序运行时没有崩溃。但当我将数字增加到 10 时,它突然崩溃了。我不知道为什么。

这是在 View Controller 中调用的函数

func loadPosts() {
isLoadingPost = true
Api.Feed.getRecentFeed(withId: Api.User.CURRENT_USER!.uid, start: posts.first?.timestamp, limit: 9 ) { (results) in
if results.count > 0 {
results.forEach({ (result) in
self.posts.append(result.0)
self.users.append(result.1)
})
}
self.isLoadingPost = false
if self.refreshControl.isRefreshing {
self.refreshControl.endRefreshing()
}
self.activityIndicatorView.stopAnimating()
self.tableView.reloadData()

}

Api.Feed.observeFeedRemoved(withId: Api.User.CURRENT_USER!.uid) { (post) in
self.posts = self.posts.filter { $0.id != post.id }
self.users = self.users.filter { $0.id != post.uid }

self.tableView.reloadData()
}
}

这是其类中的函数

func getRecentFeed(withId id: String, start timestamp: Int? = nil, limit: UInt, completionHandler: @escaping ([(Post, UserModel)]) -> Void) {

var feedQuery = REF_FEED.child(id).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: UserModel)] = []

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)** //when limit is greater than 10 - app crashes here with exc_bad_instruction error
print(index)
myGroup.leave()
})
})
}
myGroup.notify(queue: .main) {
results.sort(by: {$0.0.timestamp! > $1.0.timestamp! })
completionHandler(results)
}


})

}

当限制超过 9 时,应用程序会在星星所在的位置崩溃

最佳答案

我的想法是,您在 Api.Post.observePost 中进行观察,而不是单个观察,因此在最后一次离开发生后,将调用完成,然后对此行进行另一次调用

results.insert((post, user), at: index)

当该数组

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

由于取消分配而不存在

关于当 Firebase 查询限制超过 9 时,iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51428729/

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