ios - FirebaseTableViewDataSource 在用户注销和登录时崩溃

标签 ios swift firebase firebase-realtime-database firebaseui

我的应用有一个 UITableViewController,它使用 FirebaseTableViewDataSource(来自 FirebaseUI)。该表正确显示了用户的书签帖子,但是当我注销该用户并登录另一个用户时,应用程序崩溃并显示以下消息:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'*

我怀疑问题与 FirebaseUI 如何更新 tableView 的内容有关。过去 4 天我一直在调试它;搜索了 SO 问题;阅读相关文档,但没有人提到这个独特的问题。任何帮助将不胜感激。

详情(抱歉,其实很长)

这是我的数据库结构:

|
+-posts
|   |
|   +-$postid
|        |
|        +-/* properties of a post: title, text, etc */
|
+-users
   |
   +-$userid
        |
        +-bookmarks
             |
             +-post1: true
             |
             +-post2: true

我正在使用 FirebaseUI 通过将 users/$userid/bookmarks ref 作为查询传递给 FirebaseTableViewDataSource 来向用户显示他/她的书签。然后对于每个键,我在 posts/$postid 上观察到一个单值事件,以便检索帖子详细信息......代码如下:

self.authStateListenerHandle = FIRAuth.auth()?.addStateDidChangeListener { auth, user in

    guard let user = user else {
        return
    }

    self.query = FIRDatabase.database().reference().child("users").child(user.uid).child("bookmarks")

    self.tableViewDataSource = FirebaseTableViewDataSource(query: self.query!, view: self.tableView, populateCell: { (tableView, indexPath, snapshot) -> UITableViewCell in

        if snapshot.exists() {

            let postRef = FIRDatabase.database().reference().child("posts").child(snapshot.key)

            let cell = tableView.dequeueReusableCell(withIdentifier: BookmarksVC.reuseIdentifier, for: indexPath) as! MiniTableCell

            postRef.observeSingleEvent(of: .value, with: { (snapshot) in
                if snapshot.exists() {
                    let post = Event(snapshot: snapshot)!
                    let postVM = EventViewModel(post: post)
                    cell.populateCellWithEvent(postVM)
                    cell.delegate = self
                }
            })

            return cell
        }
        else {
            return UITableViewCell()
        }
    })

    self.tableView.dataSource = self.tableViewDataSource
}

我把上面的代码放在 viewDidAppear 中,然后像这样删除 viewWillDisappear 中的 authStateListenerHandle

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if let handle = self.authStateListenerHandle {
        FIRAuth.auth()?.removeStateDidChangeListener(handle)
    }
}

几乎一切正常,除了当我查看用户的书签,然后注销并重新登录时,应用程序崩溃并显示消息

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *

最佳答案

viewWillDisappear 中设置 self.tableViewDataSource = nil。因此,您不会不正确地更新 dataSource

关于ios - FirebaseTableViewDataSource 在用户注销和登录时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40485250/

相关文章:

ios - 如何在 Qualcomm 样本的帧标记上使用 glDrawArrays() 而不是 glDrawElements()

ios - POST ios 中的 woocommerce_api_authentication_error

android - 找不到与包名称匹配的客户端 (Google Analytics) - 多个 productFlavors/application id

typescript - 如何在 Firebase Cloud Function 可调用堆栈跟踪中显示 TypeScript 源代码?

java - 如何在应用程序加载时在 ListView 上显示从 Firebase 实时数据库检索的内容

ios - UIButton Touchdown 在 IOS 7 中是否过于敏感?

ios - iOS 设备之间的 TCP 连接

ios - iOS中不同位置的商店数据库有什么区别?

arrays - 在 swift 中初始化 json api 后出现错误

ios - 保留 UISearchBar 的边距