当我尝试在Firebase数据库中设置值时,有时我的应用会崩溃。这是我的代码:
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.")
}
}
}
我在可能主视图控制器的viewdidload中调用sendregistrationtoken()。以下是我的崩溃报告所说的内容:
+[FValidation validateFrom:validPathString]
我正在使用XCode 8 / Swift 3和最新的Firebase SDK。非常感谢 斯蒂芬