从Firebase同步读取

时间:2018-11-01 14:27:26

标签: swift firebase firebase-realtime-database synchronization

我有一个需要从Firebase读取的值,然后将它与多个其他值一起以总计两个对象的事务写入Firebase。

我正在创建一个CHAT,所以当发送消息时,我正在为两个联系人创建一个聊天室,每个联系人都是他自己的。我的代码:

private func CreateChatRoom(creatorID: String, creatorName: String ,contactID: String, contactName: String)
    {

        var creatorImageString: String = ""
        var contactImageString: String = ""
        ReadContactImage(contactID: contactID)
        {
            success in

            if success
            {
                contactImageString = self.tempContactImg
            }
        }
        ReadContactImage(contactID: creatorID)
        {
            success in

            if success
            {
                creatorImageString = self.tempContactImg
            }
        }

        let infoForCreator = [Constants.Chat.ChatRoomsLite.CONTACT_NAME: contactName,
                              Constants.Chat.ChatRoomsLite.CONTACT_ID: contactID,
                              Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS : 0,
                              Constants.Chat.ChatRoomsLite.CONTACT_IMG_URL: contactImageString] as [String : Any]


        let infoForContact = [Constants.Chat.ChatRoomsLite.CONTACT_NAME: creatorName,
                              Constants.Chat.ChatRoomsLite.CONTACT_ID: creatorID,
                              Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS : 0,
                              Constants.Chat.ChatRoomsLite.CONTACT_IMG_URL: creatorImageString] as [String : Any]

        let childUpdates = ["\(creatorID)/\(contactID)/": infoForCreator,
                            "\(contactID)/\(creatorID)/": infoForContact
                           ]

        Constants.refs.databaseChatsLite.updateChildValues(childUpdates)
    }

    private func ReadContactImage(contactID: String, completion: @escaping (Bool) -> ())
    {
        Constants.refs.databaseUsers.child(contactID).child(Constants.Account.AccountFields.USER_IMAGE_STR).observeSingleEvent(of: .value, with: {(snapshot) in

            self.tempContactImg = (snapshot.value as? String)!
            completion(true)
        })
    }

    var tempContactImg : String = "";

我在这里读到“ ReadContactImage”函数应该同步运行,但事实并非如此。所以我留下了空的联系人图像。

我考虑过只读取同一功能中的两个图像,但是CreateChatRoom也需要同步,因此基本上我会遇到相同的问题。

有人知道如何正确处理吗?

也许有更简单的方法吗?

编辑:

如果写入数据库是异步的,则在这里出现异常:

func AddChatToCollections(chatAsDictionary: NSDictionary!)
    {
        if chatAsDictionary == nil
        {
            return
        }
        let contactName = chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_NAME] as! String
        let contactImg = chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_IMG_URL] as! String
        //let lastMsg = chatAsDictionary["lastMessage"] as! String
        let newMsgs = chatAsDictionary[Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS] as! Int
        let contactID = chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_ID] as! String

        let chatToAdd = PrivateChatLiteObject(chattingWith: contactName, ContactID: contactID, unreadMessages: newMsgs, LastMSG: "", ContactImageStr: contactImg)


        chatsDictionary[contactID] = chatToAdd
        chatsIndex.append(contactID)
    }

当尝试使用字典中的信息时,该信息取自Firebase。 该函数从这里调用:

private func populateActiveChats()
    {
        let loggedOnUserID = Auth.auth().currentUser?.uid
        let ref = Constants.refs.databaseChatsLite.child(loggedOnUserID!)

        // Retrieve the products and listen for changes
        ref.observe(.value, with:
                { (snapshot) in

                    for child in snapshot.children.allObjects as! [DataSnapshot]
                    {

                        if (self.chatsDictionary.keys.contains(child.key) == false)
                        {
                            let chatValueDictionary = child.value as? NSDictionary
                            self.AddChatToCollections(chatAsDictionary: chatValueDictionary)
                            self.DispatchQueueFunc()
                        }


                    }
            })
    }

当我打开“聊天”页面时,从viewDidLoad()中呼叫哪个。

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

因为chatAsDictionary [CONTACT_NAME]不存在,因为当chatAsDictionary从Firebase获取数据时,异步功能尚未将其写入其中

1 个答案:

答案 0 :(得分:0)

这两种方法都是从Firebase异步调用加载数据的。您必须先完成对infoForCreator的两个调用,才能构造ReadContactImage(等)。

一种简单的方法是嵌套调用:

var creatorImageString: String = ""
var contactImageString: String = ""
ReadContactImage(contactID: contactID)
{
    success in

    if success
    {
        contactImageString = self.tempContactImg
        ReadContactImage(contactID: creatorID)
        {
            success in

            if success
            {
                creatorImageString = self.tempContactImg

                let infoForCreator = [Constants.Chat.ChatRoomsLite.CONTACT_NAME: contactName,
                      Constants.Chat.ChatRoomsLite.CONTACT_ID: contactID,
                      Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS : 0,
                      Constants.Chat.ChatRoomsLite.CONTACT_IMG_URL: contactImageString] as [String : Any]


let infoForContact = [Constants.Chat.ChatRoomsLite.CONTACT_NAME: creatorName,
                      Constants.Chat.ChatRoomsLite.CONTACT_ID: creatorID,
                      Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS : 0,
                      Constants.Chat.ChatRoomsLite.CONTACT_IMG_URL: creatorImageString] as [String : Any]

let childUpdates = ["\(creatorID)/\(contactID)/": infoForCreator,
                    "\(contactID)/\(creatorID)/": infoForContact
                   ]

                   Constants.refs.databaseChatsLite.updateChildValues(childUpdates)
            }
        }
    }
}

或者,您可以保留一个计数器:

var creatorImageString: String = ""
var contactImageString: String = ""
var completedCount = 0;
ReadContactImage(contactID: contactID)
{
    success in

    if success
    {
        contactImageString = self.tempContactImg
        completedCount++
        if completedCount == 2
        {
            createDatabaseNode(contactImageString, creatorImageString)
        }
    }
}
ReadContactImage(contactID: creatorID)
{
    success in

    if success
    {
        creatorImageString = self.tempContactImg
        completedCount++
        if completedCount == 2
        {
            createDatabaseNode(contactImageString, creatorImageString)
        }
    }
}

然后createDatabaseNode是一个函数,其中包含用于填充数据结构并调用updateChildValues的代码。