Swift Firebase将数据获取到类对象

时间:2019-05-29 21:09:47

标签: swift firebase firebase-realtime-database

我想获得以下结构(Firebase数据库的屏幕截图):

enter image description here

在聊天中,我具有聊天的ID。有带有子userid以及id和name值的用户。 首先,我查找用户想要并且想要获取的聊天信息,然后获取chatId(具有其ID和名称的用户)的详细信息

我在Swift中有以下课程:

class Chat {
var chatId: String!
var userIds: [String]!
var userNames: [String]!
}

我有以下代码来获取详细信息,但是我没有从chatId中获得userId或userName:

func getChatsFromFirebase() {
    self.ref = Database.database().reference()
    self.ref?.child("users").child(userdefaults.getUserId()).child("chats").observe(.childAdded, with: { (snapshot) in
        let chat = Chat()
        chat.chatId = snapshot.key
        chat.userIds = []
        chat.userNames = []

        //print(chat.chatId)

        for i in 0..<self.chats.count {
            let usersRef = self.ref.child("chats").child(self.chats[i].chatId).child("users").observeSingleEvent(of: .value, with: { (snapshot) in
                let value = snapshot.value as? NSDictionary

                for userid in value!.allKeys as! [String] {
                    let usersdetailsRef = self.ref.child("chats").child(self.chats[i].chatId).child("users").child(userid).queryOrdered(byChild: "name").observeSingleEvent(of: .value, with: { (snapshot) in

                        let value = snapshot.value as? NSDictionary
                        //print(value)
                        let id = value?["id"] as? String ?? ""
                        let name = value?["name"] as? String ?? ""
                        //print( id + ": " + name)
                        chat.userIds.append(id)
                        chat.userNames.append(name)
                    })
                }
            })
        }
        self.chats.append(chat)
        self.tableView.reloadData()
    })
}

我对Firebase主题非常陌生。有人可以帮我吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

好,您需要先更改数据模型。在这种情况下,您无需将id的值存储在12345中。您已经可以获取密钥了。另外,在/users/chats中,您只需将聊天ID保存为chat1 : IBDrbfku887BLIYIBDrbfku887BLIY : true。您始终可以分别通过值或键来获取它们。

在聊天文档中,您只需要引用用户ID,即获取它们并将其存储为user1和user2。如果您的用例需要更多用户,则可以添加更多用户。

按如下所示重新配置数据模型。

enter image description here

现在,您需要2个对象用户和聊天,如下所示:

Users.swift

class User : NSObject {

        private var _name: String!
        private var _username: String!
        private var _userid: String!
        private var _userRef: DatabaseReference!

        var name: String! {
            get {
                return _name
            } set {
                _name = newValue
            }
        }


        var username : String! {
            get {
                return _username
            } set {
                _username = newValue
            }
        }


        var userid: String! {
            get {
                return _userid
            } set {
                _userid = newValue
            }
        }

        var userRef: DatabaseReference! {
            get {
                return _userRef
            } set {
                _userRef = newValue
            }
        }

        init(userid: String, userData: Dictionary<String, Any>){

            self._userid = userid

            _userRef = Database.database().reference().child(_userid)

            if let username = userData["username"] as? String {
                self._username = username
            }

            if let name = userData["name"] as? String {
                self._name = name
            }

        }

    }

Chats.swift

class Chat : NSObject {

        private var _chatid: String!
        private var _user1: String!
        private var _user2: String!
        private var _chatRef: DatabaseReference!

        var user1: String! {
            get {
                return _user1
            } set {
                _user1 = newValue
            }
        }


        var user2 : String! {
            get {
                return _user2
            } set {
                _user2 = newValue
            }
        }


        var chatid: String! {
            get {
                return _chatid
            } set {
                _chatid = newValue
            }
        }

        var chatRef: DatabaseReference! {
            get {
                return _chatRef
            } set {
                _chatRef = newValue
            }
        }

        init(chatid: String, chatData: Dictionary<String, Any>){

            self._chatid = chatid

            _chatRef = Database.database().reference().child(_chatid)

            if let user = chatData["users"] as? Dictionary<String, Any> {
                if let user1 = user["user1"] as? String {
                    self._user1 = user1
                }
                if let user2 = user["user2"] as? String {
                    self._user2 = user2
                }
            }

        }

    }

这里的主要问题/或被忽略的问题是数据的类型。在/users中,您的ID 12345的类型为String。但是,当您从/chats获取相同内容时,它将返回为Int。这将下载该值,但不会对其进行转换。在播种/测试数据时请务必小心。

要获取用户的凭据,只需通过另一个查询引用即可。这就是你可以做的:

var allUsers = [User]()
    var allChats = [Chat]()

    func viewDidLoad() {
        super.viewDidLoad()
        fetchAllChats()
    }

    func getUser(from userId: String, completion: @escaping (User) -> Void) {

        Database.database().reference().child("users").child(userId).observeSingleEvent(of: .value, with: { snapshot in
            if let datasnap = snapshot.value as? Dictionary<String, Any> {
                let user = User(userid: userId, userData: datasnap)
                completion(user)
            }
        })
    }

    func fetchAllChats() {
        Database.database().reference().child("chats").observeSingleEvent(of: .value, with: { snapshot in
           allChat.removeAll()
            if let snapshot = snapshot.value as? Dictionary<String, Any> {
                for snap in snapshot {
                    if let chatd = snap.value as? Dictionary<String, Any> {
                        let chat = Chat(chatid: snap.key, chatData: chatd)
                        self.allChats.append(chat)
                    }
                }
            }
            // collectionview.reloadData() <--------- only if required.
        })
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let chatData = allChats[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath) as! Cell
        getUser(from: chatData.user1) { user in
            cell.label.text = user.usernme
        }
        return cell
    }