【问题标题】:Unpacking Firestore array with objects to a model in swift快速将带有对象的 Firestore 数组解包到模型中
【发布时间】:2024-05-23 03:25:01
【问题描述】:

我有一个快速使用 Firestore 的数据库项目。我的用户的 Firestore 数据集如下所示。包含对象的数组的用户详细信息。

我有一个函数可以获取具有所有 firestore 数据的特定用户:

func fetchUser(){
    
    db.collection("users").document(currentUser!.uid)
        .getDocument { (snapshot, error ) in
            do {
                if let document = snapshot {
                    
                    let id = document.documentID
                    let firstName = document.get("firstName") as? String ?? ""
                    let secondName = document.get("secondName") as? String ?? ""
                    let imageUrl = document.get("imageUrl") as? String ?? ""
                    let joinedDate = document.get("joinedDate") as? String ?? ""
                    let coins = document.get("coins") as? Int ?? 0
                    let challenges = document.get("activeChallenges") as? [Challenge] ?? []
                    
                    let imageLink = URL(string: imageUrl)
                    
                    let imageData = try? Data(contentsOf: imageLink!)
                    
                    let image = UIImage(data: imageData!) as UIImage?
                    
                    let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 }
                    print("array without opt", arrayWithNoOptionals)
                    
                    self.user = Account(id: id, firstName: firstName, secondName: secondName, email: "", password: "", profileImage: image ?? UIImage(), joinedDate: joinedDate, coins: coins, activeChallenges: challenges)
                }
                else {
                    print("Document does not exist")
                    
                }
            }
            catch {
                fatalError()
            }
        }
}

这是用户模型的样子:

class Account {
var id: String?
var firstName: String?
var secondName: String?
var email: String?
var password: String?
var profileImage: UIImage?
var coins: Int?
var joinedDate: String?
var activeChallenges: [Challenge]?


init(id: String, firstName: String,secondName: String, email: String, password: String, profileImage: UIImage, joinedDate: String, coins: Int, activeChallenges: [Challenge]) {
    self.id = id
    self.firstName = firstName
    self.secondName = secondName
    self.email = email
    self.password = password
    self.profileImage = profileImage
    self.joinedDate = joinedDate
    self.coins = coins
    self.activeChallenges = activeChallenges
}

init() {
    
}

}

问题是我不明白如何将活动挑战从 firestore 映射到模型数组。当我尝试时: let challenges = document.get("activeChallenges") as? [Challenge] ?? [] 打印包含一个空数组,但是当我这样做时:let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 } print("array without opt", arrayWithNoOptionals)

这是平面图的输出: 它返回一个可选数组

【问题讨论】:

  • 你不能投到[挑战],你需要使用.map

标签: swift firebase google-cloud-firestore


【解决方案1】:

系统无法知道 activeChallenges 是 Challenge 对象的数组。因此,您需要先将其转换为键值类型(字典),然后将其映射到 Challenge 对象

let challengesDict = document.get("activeChallenges") as? [Dictionary<String: Any>] ?? [[:]]
let challenges = challengesDict.map { challengeDict in
    let challenge = Challenge()
    challenge.challengeId = challengeDict["challengeId"] as? String
    ...
    return challenge
}

这与将 snapshot(document) 转换为 Account 对象的方式相同

【讨论】:

    最近更新 更多