FirebaseUI-iOS: swift 3, iOS Firebase Database sometimes crash when writing data (set value)

sometimes my app is crashing when I´m trying to set a value in a Firebase Database. Here´s my code:

var databaseRef = FIRDatabaseReference()

func sendregistrationtoken() {
    print("tokenRefreshNotification")

    guard let contents = FIRInstanceID.instanceID().token()
        else {
            return
    }

    if let userusername: String = Keychain().accessibility(.afterFirstUnlock)["userusername"] {
        if let refreshedToken = FIRInstanceID.instanceID().token(){
            let defaults = UserDefaults.standard
            defaults.set(refreshedToken, forKey: "userregistrationtoken")
            let userid = userusername.replacingOccurrences(of: ".", with: "%2E")
            let post : [String:AnyObject] = ["token" : refreshedToken as AnyObject]

            databaseRef = FIRDatabase.database().reference()
            databaseRef.child("Users").child(userid.uppercased()).setValue(post){ (error, ref) -> Void in
                print(error ?? "noerror")
            }

            connectToFcm()
        }
    }

}

func connectToFcm() {
    print("connectToFcm")
    FIRMessaging.messaging().connect { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}

I call sendregistrationtoken() in my viewdidload of may main view controller. Here´s what my crash report says:

+[FValidation validateFrom:validPathString]

Many Thanks Stephan

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Actually, what I mean is that, even if this is a programmer error (in my case the string was well formated but there was no child with this ID on the database) an external library (an SDK) should never make the app crash. It may give you an error telling you that this child doesn’t exists, but not making the whole app crash without the chance to control it.

Anyway, I will fill the issue form. Thank you!

I can’t say without the full exception message, but your code will crash on this line

databaseRef.child("Users").child(userid.uppercased())

if userid.uppercased() isn’t a valid path string.