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

标签 ios swift firebase firebase-realtime-database

正如标题所示,我调用 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/

相关文章:

android - Firebase 云消息传递如何在 Android 应用程序中初始化

ios - 为什么 NSURL Connection Received Data 包含字节,但在转换为 NSString 时,字符串为 Null?

swift - 如何将 View 元素的func()值传递给另一个Controller

ios - 如果没有返回搜索结果,如何在 UITableView 中显示 "No results found"?

swift - 谁的 View 仅在首次启动时不在窗口层次结构中

Firebase 云函数 onWrite 子值

android - 无法从 Firebase gcloud 命令行启动仪器测试

ios - 使用 GCD 复制文件的最有效方法?

ios - 出现键盘时向上移动 View

ios - 以编程方式在 collectionView swift 中无限滚动