Firebase无法接受一系列值,因此我想出了将每个值添加到其自己的个人密钥中的想法。每次输入新值时,键都会增加一个递增的计数。
即
@IBOutlet weak var colorNameTextField: UITextField!
var increaseColorKey = "colorKey"
var count = 0
var ref: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
self.ref = FIRDatabase.database().reference()
}
@IBAction func sendToFirebaseButton(sender: UIButton) {
let newCount = count += 1
let newKey = increaseColorKey + String(newCount)
var dict = [String:AnyObject]()
dict.updateValue(self.colorNameTextField.text!, forKey: "newKey")
let colorRef = self.ref.child("colors")
colorRef.updateChildValues(dict)
let dictKey = Array(dict.keys)
print("\(newKey)") //prints colorKey1
print("\(dictKey)") //prints ["colorKey1"]
}
Firebase内部我现在有
root
|
@--colors
|__colorkey1: "user text.."
如果用户在3个不同的场合输入了3种颜色,那么Firebase的结果将是:
root
|
@--colors
|__colorkey1: "user text..."
|__colorkey2: "user text..."
|__colorkey3: "user text..."
我现在看到的问题是,如果用户切换设备,那么上面的计数变量可能与Firebase内的计数完全不同。
问题是我应该在firebase中有一个计数值,然后先将其拉出并从那里开始增加,还是应该以另一种方式进行?我怎样才能解决这个问题?