Firebase Firestore 数据库 setData 与自定义模型

时间:2021-08-01 20:31:43

标签: swift firebase firebase-realtime-database

我想将数据从 RealtimeDatabase 打印到 FirestoreDatabase。但是,有一个问题。当我用for返回它时,我不时收到以下错误。

错误:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

如何以最短的方式执行此操作而不使用 For 返回?

我可以在不写 [String : Any] 的情况下将数据设置到数据库中吗?我怎样才能以最短的方式做到这一点?我喜欢这种方法,因为我的 for 操作不起作用。

Firestore:

Firestore.firestore().collection("users").document(user.uid).setData(
      [question.title ?? "" : [["title" : question.title ?? ""],
                               ["question" : question.test?.compactMap({$0.question})],
                               ["isQuestionImage" : question.test?.compactMap({$0.isQuestionImage})],
                               ["isSectionImage" : question.test?.compactMap({$0.isSectionImage})],
                               ["imageURL" : question.test?.compactMap({$0.imageURL})],
                               ["test" : [["sections" : [
                                  "A" : question.test?.compactMap({$0.sections?.A}),
                                  "B" : question.test?.compactMap({$0.sections?.B}),
                                  "C" : question.test?.compactMap({$0.sections?.C}),
                                  "D" : question.test?.compactMap({$0.sections?.D})
                               ]]
                               ]],
                               ["correct" : question.test?.compactMap({$0.correct})]
 
      ]], merge: true)

问题模型:

struct QuizContainer: Codable, Hashable {
    let allQuiz: [Quiz]?
}

struct Quiz: Codable, Hashable {
    let title: String?
    let test: [Test]?
}

struct Test: Codable, Hashable {
    let id: Int?
    let question: String?
    let isQuestionImage, isSectionImage: Bool?
    let imageName: String?
    let sections: Sections?
    let correct: String?
}

struct Sections: Codable, Hashable {
    let A, B, C, D: String?

    enum CodingKeys: String, CodingKey {
        case A = "A"
        case B = "B"
        case C = "C"
        case D = "D"
    }
}

用户模型:

struct User: Codable {
    var uid: String
    var questions: QuizContainer?
    var wrongQuestions: [Test]?
}

功能:

func foo(user: User) {
        Database.database().reference().observeSingleEvent(of: .value) { snapshot in
            
            do {
                let questions = try FirebaseDecoder().decode(QuizContainer.self, from: snapshot.value ?? "")
                for question in questions.allQuiz ?? [] {
                    
                    Firestore.firestore().collection("users").document(user.uid).setData(
                        [question.title ?? "" : [["title" : question.title ?? ""],
                                                 ["question" : question.test?.compactMap({$0.question})],
                                                 ["isQuestionImage" : question.test?.compactMap({$0.isQuestionImage})],
                                                 ["isSectionImage" : question.test?.compactMap({$0.isSectionImage})],
                                                 ["imageURL" : question.test?.compactMap({$0.imageURL})],
                                                 ["test" : [["sections" : [
                                                    "A" : question.test?.compactMap({$0.sections?.A}),
                                                    "B" : question.test?.compactMap({$0.sections?.B}),
                                                    "C" : question.test?.compactMap({$0.sections?.C}),
                                                    "D" : question.test?.compactMap({$0.sections?.D})
                                                 ]]
                                                 ]],
                                                 ["correct" : question.test?.compactMap({$0.correct})]
                                                 
                        ]], merge: true)
                    
                }
            } catch {
                print(error.localizedDescription)
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。

https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects

具体来说,这使您可以访问一个特殊的 setData 调用,它看起来像 document("LA").setData(from: city)。

在 Firebase v7.3+ 中您应该不再需要额外的导入,这太棒了!如果您仍在使用 v6 或更早版本,则需要它。

如果您无法在代码中访问此函数,那是因为您需要导入 FirebaseFirestoreSwift,它是 v7.0 之前的 Swift 的特殊扩展。

for value in questions.test ?? [] {
                        
                        self.user.questions?.title = selectedTest != "" ? selectedTest : selectedTest
                        self.user.questions?.test?.append(Test(id: value.id, question: value.question, isQuestionImage: value.isQuestionImage, isSectionImage: value.isSectionImage, imageURL: value.imageURL, sections: value.sections, correct: value.correct))
                        self.user.wrongQuestions = []
                    }