1

Hi i'm building my app with Firebase and Swift3 , but i get this error when i run the code "(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''" . This is the func where i get the error, even if everything should work

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print (self.otherUserUid)
    let reference = Database.database().reference().child("User-messages").child(Auth.auth().currentUser!.uid).child(self.otherUserUid).queryLimited(toLast: 51)
    reference.observeSingleEvent(of: .value, with: {[weak self] (snapshot) in
        print("IN")
        let messages = Array(JSON(snapshot.value as Any).dictionaryValue.values).sorted(by: { (lhs, rhs) -> Bool in
            return lhs["date"].doubleValue < rhs["date"].doubleValue

        })
        let converted = self!.convertToChatItemProtocol(messages: messages)
        if converted.isEmpty == true {
            print ("empty")
        }
        let navcontroller = segue.destination as! UINavigationController
        let chatlog = navcontroller.topViewController as! ChatLogController
        chatlog.userUID = self.otherUserUid
        chatlog.dataSource = DataSource(initialMessages: converted ,uid: (self?.UIDLabel.text)!)
        chatlog.MessagesArray = FUIArray(query: Database.database().reference().child("User-messages").child(Me.uid).child((self?.UIDLabel.text)!).queryStarting(atValue: nil, childKey: converted.last?.uid), delegate: nil)


        messages.filter({ (message) -> Bool in
            return message["type"].stringValue == PhotoModel.chatItemType
        }).forEach({ (message) in
            self?.parseURLs(UID_URL: (key: message["uid"].stringValue, value: message["image"].stringValue))
        })

    })

}

The app crashes before printing "IN", but when i print self.otherUserUid it returns the right value. I also updated the pods but it didn't solved anything. Thanks in advance :)

SOLUTION : The mistake is that i inserted a func in the viewDidLoad of the ChatLogController, where when an user decides to end the chat the other one gets informated, but that func needs the other user Uid and when the view is loading there isn't any uid because it has to be passed, so i inserted the func inside "viewDidAppear" and everything works fine. Thanks to everyone that tried to help me.

Luca M.
  • 21
  • 1
  • 5

2 Answers2

2

This is happening because this symbols '.' '#' '$' '[' or ']' are forbiden when it comes to Firebase. A key in Firebase cannot contains any of these symbols. So in order to solve this, you need to encode those Strings by replacing those symbols with other symbols that are permitted. For example, for an email address i recomand you make this change.

name@email.com -> name@email,com

As you probably see, i have replaced the . (dot) with a , (comma).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yes i know, i have already checked for this issue and all the strings are without any of this symbols '.' '#' '$' '[' or ']' because every child is only an user uid, that is only a string. Thanks anyway. – Luca M. Sep 19 '17 at 15:08
  • Check also for non-empty string. Does it have a value? – Alex Mamo Sep 19 '17 at 15:14
1

For me the problem was that I had Double values casted as Strings. But when you look more specific to Double values they have a dot included e.g. "1234.56" so Firebase was yelling at me. I replaced that dot with a comma "1234,56" and it worked out.

For replacing the dot with a comma a can suggest this post:

https://stackoverflow.com/a/24201206/8165702