Swift 如何创建多页表单并将数据保存到 Firebase?
Swift how to Create Multipage Form and Save Data to Firebase?
我已经成功安装了Firebase并且连接了UID的登录和注册。如果用户在应用程序中保存了额外的数据,我现在想将其分配给登录的相应用户,最好的方法是什么?抱歉,我是 Swift 和 Firebase 的初学者,我需要一个不太复杂的教程或解释。
谢谢大家
- Uikit
- Swift 5
- Firebase
所有这一切都假设您已将 Firebase UserAuth 连接到您的应用和设置。
所有用户都有一个唯一标识他们的 UID,即用户标识符。这很容易得到。
//there must be a user signed in
let user = Auth.auth().currentUser
let uid = user.uid
简单来说,要存储用户独有的数据,使用Firestore将其全部存储在uid下。如果您没有 Firestore,get started with Firestore.
您保存到 Firestore 的所有数据都必须以字典格式构建,以 String 作为键,Any 作为值。例如,如果我想为用户存储前 3 种最喜欢的冰淇淋口味,你可以这样做 *注意 firebase 会自动为你创建这些文档和集合,如果它们不存在,所以不要惊慌 *:
//First get a reference to the database.
// It is best to have db as a global variable
let db = Firestore.firestore()
let favoriteFlavors: [String: Any] = ["numberOne":flavorOne as Any, "numberTwo":flavorTwo as Any, "numberThree": flavorThree as Any]
//access the collection of users
//access the collection of the currentUser
//create a document called favoriteFlavors
//set the document's data to the dictionary
db.collection("users").collection(uid).document("favoriteFlavors").setData(favoriteFlavors) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
现在,当您想要检索此数据时,您确实访问了用户集合,即已登录用户的集合,然后读取 favoriteFlavors 文档——像这样:
let docRef = db.collection("users").collection(uid).document("favoriteFlavors")
docRef.getDocument { (document, error) in
if let document = document {
print("Document received")
// retrieve the data in the document (a dictionary)
let data = document.data()
} else {
print("Document does not exist")
}
}
所以如果你想获得最受欢迎的第一口味,你会这样做:
//Remember that data is a dictionary of String:Any
if let numberOneFlavor = data["numberOne"] as? String {
print("Number one favorite flavor ",numberOneFlavor)
}
诚然,这可能会变得更加复杂,但这是您需要了解的内容的坚实基础。我建议阅读 Firestore 文档的 add data and get data 页。
我已经成功安装了Firebase并且连接了UID的登录和注册。如果用户在应用程序中保存了额外的数据,我现在想将其分配给登录的相应用户,最好的方法是什么?抱歉,我是 Swift 和 Firebase 的初学者,我需要一个不太复杂的教程或解释。
谢谢大家
- Uikit
- Swift 5
- Firebase
所有这一切都假设您已将 Firebase UserAuth 连接到您的应用和设置。
所有用户都有一个唯一标识他们的 UID,即用户标识符。这很容易得到。
//there must be a user signed in
let user = Auth.auth().currentUser
let uid = user.uid
简单来说,要存储用户独有的数据,使用Firestore将其全部存储在uid下。如果您没有 Firestore,get started with Firestore.
您保存到 Firestore 的所有数据都必须以字典格式构建,以 String 作为键,Any 作为值。例如,如果我想为用户存储前 3 种最喜欢的冰淇淋口味,你可以这样做 *注意 firebase 会自动为你创建这些文档和集合,如果它们不存在,所以不要惊慌 *:
//First get a reference to the database.
// It is best to have db as a global variable
let db = Firestore.firestore()
let favoriteFlavors: [String: Any] = ["numberOne":flavorOne as Any, "numberTwo":flavorTwo as Any, "numberThree": flavorThree as Any]
//access the collection of users
//access the collection of the currentUser
//create a document called favoriteFlavors
//set the document's data to the dictionary
db.collection("users").collection(uid).document("favoriteFlavors").setData(favoriteFlavors) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
现在,当您想要检索此数据时,您确实访问了用户集合,即已登录用户的集合,然后读取 favoriteFlavors 文档——像这样:
let docRef = db.collection("users").collection(uid).document("favoriteFlavors")
docRef.getDocument { (document, error) in
if let document = document {
print("Document received")
// retrieve the data in the document (a dictionary)
let data = document.data()
} else {
print("Document does not exist")
}
}
所以如果你想获得最受欢迎的第一口味,你会这样做:
//Remember that data is a dictionary of String:Any
if let numberOneFlavor = data["numberOne"] as? String {
print("Number one favorite flavor ",numberOneFlavor)
}
诚然,这可能会变得更加复杂,但这是您需要了解的内容的坚实基础。我建议阅读 Firestore 文档的 add data and get data 页。