Ios 应用程序将数据重置为零,即使其存储在firebase上
我有一个应用程序,基本上是一个计算器,用户可以将他们从某些游戏中赢得的总金额相加。当我输入一个不是整数的数字时,例如“$1.25”,它会在firebase上保存为“$1.25”,但当我关闭应用程序并重新打开时,该值会重置为$0.00,但在firebase上仍存储为$1.25”。 如果我用一个整数做同样的事情,比如说“$1”,然后关闭应用程序并打开,那么“$1”将保留在应用程序上Ios 应用程序将数据重置为零,即使其存储在firebase上,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我有一个应用程序,基本上是一个计算器,用户可以将他们从某些游戏中赢得的总金额相加。当我输入一个不是整数的数字时,例如“$1.25”,它会在firebase上保存为“$1.25”,但当我关闭应用程序并重新打开时,该值会重置为$0.00,但在firebase上仍存储为$1.25”。 如果我用一个整数做同样的事情,比如说“$1”,然后关闭应用程序并打开,那么“$1”将保留在应用程序上 //add local data let gameKey = gameNames[addWinning
//add local data
let gameKey = gameNames[addWinningsView.tag]
let newValue = amountList[addWinningsView.tag] + (Double(addWinningsAmount.text!) ?? 0)
UserDefaults.standard.set(newValue, forKey: gameKey)
UserDefaults.standard.synchronize()
//Update UI
loadTotalAmount()
//update to firebase
let uuid = UIDevice.current.identifierForVendor!.uuidString
let ref = Database.database().reference().child("app_data/user_total_won/\(uuid)/\(gameKey)")
ref.setValue(newValue)
从firebase加载数据的代码
//Load winning points
let uuid = UIDevice.current.identifierForVendor!.uuidString
Database.database().reference().child("app_data/user_total_won/\(uuid)").observeSingleEvent(of: .value, with: {(snapshot) in
let dict = snapshot.value as? [String : AnyObject] ?? [:]
for game in gameNames {
let amount = dict[game] as? Int ?? 0
UserDefaults.standard.set(amount, forKey: game)
UserDefaults.standard.synchronize()
}
self.loadTotalAmount()
}
预期结果:从firebase检索的数据不仅仅是一个整数。从firebase检索值时,可选择将其转换为?Doublenot
Int
,这样您就不会只获取整数
要解决此问题,请将
let amount=dict[game]as?Int??0
更改为let amount=dict[game]as?Double??0
如果您在Firestore中将金额保存为Double
。查询数据库以显示当前值的代码是什么?我做了编辑:)