gpt4 book ai didi

swift - Firestore - Swift 子集合

转载 作者:行者123 更新时间:2023-11-30 11:31:59 26 4
gpt4 key购买 nike

因此,我正在尝试学习 Firestore 的一些基本功能,并观看了 YouTube 上解释 CRUD 操作的“Kilo Locos”视频。我想采用他的代码方法并从中创建子集合。基本上,我如何添加一个集合并使“用户”集合成为这个新集合的子集合。非常感谢任何帮助,非常感谢!!

这里是下载该项目的链接: https://kiloloco.com/courses/youtube/lectures/3944217

FireStore服务

import Foundation
import Firebase
import FirebaseFirestore

class FIRFirestoreService {

private init() {}
static let shared = FIRFirestoreService()

func configure() {
FirebaseApp.configure()
}

private func reference(to collectionReference: FIRCollectionReference) -> CollectionReference {
return Firestore.firestore().collection(collectionReference.rawValue)
}

func create<T: Encodable>(for encodableObject: T, in collectionReference: FIRCollectionReference) {
do {
let json = try encodableObject.toJson(excluding: ["id"])
reference(to: collectionReference).addDocument(data: json)

} catch {
print(error)
}
}

func read<T: Decodable>(from collectionReference: FIRCollectionReference, returning objectType: T.Type, completion: @escaping ([T]) -> Void) {

reference(to: collectionReference).addSnapshotListener { (snapshot, _) in

guard let snapshot = snapshot else { return }

do {

var objects = [T]()
for document in snapshot.documents {
let object = try document.decode(as: objectType.self)
objects.append(object)
}

completion(objects)

} catch {
print(error)
}
}
}

func update<T: Encodable & Identifiable>(for encodableObject: T, in collectionReference: FIRCollectionReference) {

do {

let json = try encodableObject.toJson(excluding: ["id"])
guard let id = encodableObject.id else { throw MyError.encodingError }
reference(to: collectionReference).document(id).setData(json)

} catch {
print(error)
}
}

func delete<T: Identifiable>(_ identifiableObject: T, in collectionReference: FIRCollectionReference) {

do {

guard let id = identifiableObject.id else { throw MyError.encodingError }
reference(to: collectionReference).document(id).delete()

} catch {
print(error)
}
}
}

FIRCollectionReference

import Foundation

enum FIRCollectionReference: String {
case users
}

用户

import Foundation

protocol Identifiable {
var id: String? { get set }
}

struct User: Codable, Identifiable {
var id: String? = nil
let name: String
let details: String

init(name: String, details: String) {
self.name = name
self.details = details
}
}

可编码扩展

import Foundation

enum MyError: Error {
case encodingError
}

extension Encodable {

func toJson(excluding keys: [String] = [String]()) throws -> [String: Any] {

let objectData = try JSONEncoder().encode(self)
let jsonObject = try JSONSerialization.jsonObject(with: objectData, options: [])
guard var json = jsonObject as? [String: Any] else { throw MyError.encodingError }

for key in keys {
json[key] = nil
}

return json

}

}

快照扩展

import Foundation
import FirebaseFirestore

extension DocumentSnapshot {

func decode<T: Decodable>(as objectType: T.Type, includingId: Bool = true) throws -> T {

var documentJson = data()
if includingId {
documentJson!["id"] = documentID
}

let documentData = try JSONSerialization.data(withJSONObject: documentJson!, options: [])
let decodedObject = try JSONDecoder().decode(objectType, from: documentData)

return decodedObject
}
}

最佳答案

Firestore 结构不能将集合作为其他集合的子集合。

您的问题(如何添加集合并使“用户”集合成为此新集合的子集合?)的答案是您不能。相反,您必须在这两个集合之间放置一个文档。

<小时/>

Read this for more information.

它说:注意集合和文档的交替模式。您的收藏和文档必须始终遵循此模式。您不能引用集合中的集合或文档中的文档。

关于swift - Firestore - Swift 子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com