iShaalan iShaalan - 2 months ago 10
iOS Question

SetValue is not behaving as expected

I am using Firebase in an iOS app written using Swift.

static func createUserRecord(uid: String, user: User){
let userDictionary = user.getJSON()
self.usersRef.child(uid).setValue(userDictionary)
}


This code was working just fine before I release my app and after releasing the app I could not get user records added anymore, All other functionalities of the app are working fine, including saving and retrieving some data. Can anyone spot the error in this code?

Does
setValue
works even if the path was not there ? e.g. if the path
a/b/
is there, and I called
a/b/c.child('d').setValue("abcd")
. Will this work?

Answer

Calling setValue() will create the path if it doesn't exist yet.

It seems more likely that your user.getJSON() contains something that is invalid according to the database (or your security rules).

You'll want to attach a completion block to detect this.

For an example of how to do this, see Detect errors when writing a value on iOS.