大佬教程收集整理的这篇文章主要介绍了迅速。 Firestore,通过 referenceID 获取文档,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Firestore 的新手,我不清楚如何通过引用获取文档。例如
Users(collection): documents: ID: 123413 -> name: "test1",lastname: "test1" ID: we4321 -> name: "test2",lastname: "test2" ID: dsf234 -> name: "test3",lastname: "test3" ID: qwe124 -> name: "test4",lastname: "test4"
@H_489_7@例如新集合
Animals(collection): documents: ID: rewerwr -> name: "Dog1",ref: "/documents/123413" ID: sad3432 -> name: "Dog2",ref: "/documents/we4321" ID: q4324da -> name: "Dog3",ref: "/documents/dsf234" ID: safdsf2 -> name: "Dog4",ref: "/documents/qwe124"
@H_489_7@我如何获得用户
ID: 123413 -> name: "test1",lastname: "test1"
动物ID: rewerwr -> name: "Dog1",ref: "/documents/123413"
例如获取用户 -
test1
将是这样的
Firestore.firestore().collection("Users").document("123413")
但我如何为该用户获取动物?
解决方法
Firestore 可以一次从单个集合加载数据,因为没有可用的连接。如果你想显示哪个动物属于哪个用户,你必须在你的代码中实现它。
,
您可以参考以下文章no sql data modeling我建议将您的
Animal
模型稍微更改为:struct User: Codable,Identifiable { @DocumentID var id: String? var name: String var lastName: String } struct Animal: Codable,Identifiable { @DocumentID var id: String? var name: String var owner: User }
@H_489_7@这样,当您检索
Animal
文档时,您将在该文档中拥有所有者信息。所以:animalDocumentReference.getDocument { docSnapshot,error in guard error == nil else { return } guard let animal = try? docSnapshot!.data(as: Animal.self) else { return } animal.owner // You can now set this value to an @Published property or do whatever you want with it }
@H_489_7@如果这更符合您的目的,您也可以反过来这样做。在
pets
中有一个名为User
的属性。userDocumentReference.getDocument { docSnapshot,error in guard error == nil else { return } guard let user = try? docSnapshot!.data(as: User.self) else { return } user.pets // You can now set this value to an @Published property or do whatever you want with it }
@H_489_7@大佬总结
以上是大佬教程为你收集整理的迅速。 Firestore,通过 referenceID 获取文档全部内容,希望文章能够帮你解决迅速。 Firestore,通过 referenceID 获取文档所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。