Swift 如何创建多页表单并将数据保存到 Firebase?

发布于 2025-01-15 07:42:47 字数 197 浏览 0 评论 0原文

我已经成功安装Firebase并使用UID连接登录和注册。如果用户在应用程序中保存其他数据,我现在想将其分配给登录的相应用户,最好的方法是什么?抱歉,我是 Swift 和 Firebase 的初学者,我需要一个不太复杂的教程或解释。

谢谢大家

  • Ukit
  • Swift 5
  • Firebase

I have successfully installed Firebase and connected the login and registration with UID. If the user saves additional data in the app, I would now like to assign this to the respective user who is logged in, what is the best way to do this? Sorry I'm a beginner in Swift and Firebase, I need a tutorial or an explanation that is not too complex.

Thank you all

  • Uikit
  • Swift 5
  • Firebase

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2025-01-22 07:42:47

所有这一切都假设您已将 Firebase UserAuth 连接到您的应用程序和设置。

所有用户都有一个 UID(用户标识符)来唯一标识他们。这很容易得到。

//there must be a user signed in
let user = Auth.auth().currentUser
let uid = user.uid

简而言之,要存储用户独有的数据,请使用 Firestore 将其全部存储在 uid 下。如果您没有 Firestore,请开始使用 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 文档页面。

All of this is assuming you have Firebase UserAuth connected with your app and setup.

All users have a UID, user identifier, that uniquely identifies them. This is easy to get.

//there must be a user signed in
let user = Auth.auth().currentUser
let uid = user.uid

Simply put, to store data that is unique to the user, store it all under the uid using Firestore. If you do not have Firestore, get started with Firestore.

All data you save to Firestore, must be structured in a dictionary format with String as the key and Any as the value. For example, if I wanted to store the top 3 favorite flavors of ice cream for a user, you would do this *note firebase will create these documents and collections for you automatically if they do not exist so do not panic *:

//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!")
    }
}

Now, when you want to retrieve this data, you do access the users collection, the collection of the logged in user, then read the favoriteFlavors document--like this:

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")
    }
}

So if you wanted to get the number one favorite flavor, you would do this:

//Remember that data is a dictionary of String:Any
if let numberOneFlavor = data["numberOne"] as? String {
    print("Number one favorite flavor ",numberOneFlavor)
}

Granted, this can get more convoluted, but this is a solid foundation of what you need to know. I advice reading the add data and get data pages of the Firestore documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文