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

时间:2018-07-19 17:31:19

标签: ios swift firebase firebase-realtime-database

就像标题中所说的那样,我以查询返回限制为9调用firebase数据库,并且该应用程序运行时没有崩溃。但是当我将数字增加到10时,它突然崩溃了。我不知道为什么。

这是在视图控制器中调用的函数

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时,应用会在星星所在的位置崩溃

1 个答案:

答案 0 :(得分:0)

我想到的是,您在LastName, FirstName - Unit <first.last@SomeDomain.com>; LastName, FirstName - Unit <first.last@SomeDomain.com>; LastName, FirstName - Unit <first.last@SomeDomain.com>; FirstName - Unit <first.last@SomeDomain.com>; FirstName <first.last@SomeDomain.com>; LastName, FirstName - Unit <first.last@SomeDomain.com>; LastName - Unit <first.last@SomeDomain.com>; LastName, FirstName - Unit <first.last@SomeDomain.com>; 中进行了观察,而不是一次观察,因此在发生最后一次休假后,调用了complete,然后再次调用了该行

Api.Post.observePost

该数组何时

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

由于取消分配而不存在

相关问题