【问题标题】:Swift 3 Firebase update code convertion - sortingSwift 3 Firebase 更新代码转换 - 排序
【发布时间】:2016-09-26 14:19:51
【问题描述】:

不久前我在 Udemy 上注册了一门课程,内容是关于如何使用 Swift 和 Firebase 创建聊天应用程序,只是因为我想深入了解它是如何工作的。

但是,我在一周后完成了课程。这是几个月前的事了,从那时起 - Firebase 和 Swift 都有更新,我目前正在努力将一些旧代码转换为新代码。

这是我目前正在努力解决的代码:

func loadRecents() {

        firebase.child("Recent").queryOrdered(byChild: "userId").queryEqual(toValue: FIRAuth.auth()!.currentUser?.uid).observe(.value) { (snapshot:FIRDataSnapshot) in

            self.recents.removeAll()

            if snapshot.exists() {

                if let values = snapshot.value as? [String:AnyObject] {

                    let sorted = (values as! NSArray).sortedArray(using: [NSSortDescriptor(key: "date", ascending: false)])


                    for recent in sorted {

                        self.recents.append(recent as! NSDictionary)
                        self.conversationTableView.reloadData()

                    }

                }

                self.conversationTableView.reloadData()

            }

            self.conversationTableView.reloadData()

        }

        self.checkUnread()

    }

所以这只是在 tableView 中显示所有“聊天”的代码,我只是通过 Date 对它进行排序,其中升序为 false。

我所做的更改只是将旧的snapshot.value.allValues 转换为snapshot.value as? [String:AnyObject]。查询按登录账号(FIRAuth.auth().currentUser!.uid)排序。

但是,我很确定此代码已被弃用,因为每次构建此代码时都会出错。而且我知道它在以前的版本中有效。

这是错误:

Thread 1: EXC_BAD_INSTRUCTION

如果有必要,并且碰巧我完全搞砸了当前代码(可能是这样),这里是完整的旧代码:

 func loadRecents() {
        firebase.child("Recent").queryOrderedByChild("userId").queryEqualToValue(FIRAuth.auth()!.currentUser!.uid).observeEventType(.Value, withBlock: {
            snapshot in

            self.recents.removeAll()

            if snapshot.exists() {

                let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "date", ascending: false)])

                for recent in sorted {

                    self.recents.append(recent as! NSDictionary)

                    //add functio to have offline access as well, this will download with user recent as well so that we will not create it again

                    firebase.child("Recent").queryOrderedByChild("chatRoomID").queryEqualToValue(recent["chatRoomID"]).observeEventType(.Value, withBlock: {
                        snapshot in
                    })

                }

            }

            self.tableView.reloadData()
        })

        self.checkUnread()

    }

关于如何绕过/修复此问题的任何想法?非常感谢您的帮助。

【问题讨论】:

    标签: ios swift sorting firebase firebase-realtime-database


    【解决方案1】:

    你必须使用 Dictionary()

    如果让值 = (snapshot.value) 为!字典

    【讨论】:

      【解决方案2】:

      我不知道 Firebase 结构,但如果数据如下所示,这里有一个解决方案:

      [
        ("post_42", ["date": "2016-09-16"]),
        ("post_12", ["date": "2016-09-19"]), 
        ("post_87", ["date": "2016-09-04"])
      ]
      

      以及一些用于打印按日期排序的数据的代码(这将进入块/闭包内)

      let dict = snapshot!.value as! [String:[String:String]]
      
      //print the unordered data from the dictionary
      for item in dict {
           print(item)
      }
      
      let sortedArray = Array(dict).sorted { $0.1["date"]! < $1.1["date"]! }
      
      //print the data sorted by date               
      for sortedItem in sortedArray {
           print(sortedItem)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        相关资源
        最近更新 更多