[英]Issues getting @DocumentID id from a generic swift declaration of a Firebase Firestore for CRUD
Models have been declared with an @DocumentID var id: String?模型已使用 @DocumentID var id: String? Property and on update or delete methods I get error:
属性和更新或删除方法我得到错误:
"Type of expression is ambiguous without more context" “没有更多上下文,表达的类型是模棱两可的”
All model adopt the same protocol which is the type the Generic expects when passing obj.所有 model 都采用相同的协议,这是 Generic 在传递 obj 时所期望的类型。
Anyone have any insight or idea as to how to get the id from a generic?任何人都对如何从泛型获取 id 有任何见解或想法? Has anyone encountered this issue?
有没有人遇到过这个问题?
func remove(_ obj:T)
{
guard let documentID = obj.id else { return }
self.collection.document(documentID).delete
{
error in
if let error = error
{
print("Unable to remove \(error.localizedDescription).")
}
}
}
Issue 2: due to another issue regarding parameter initialization before self available I am trying to initialize the generic firestorm in a view model:问题 2:由于在自我可用之前有关参数初始化的另一个问题,我正在尝试在视图 model 中初始化通用 firestorm:
class ProtosListViewModel<T: Decodable, ObservableObject>
{
@Published var firestoreManager:FirestoreManager<T> where T:ProtoType
init()
{
let protoType = ProtoType(collectionPath: "protoName")
self.firestoreManager = FirestoreManager(database: Firestore.firestore(), firestoreType: protoType)
}
}
and getting error: "Type 'T' does not conform to protocol 'ProtoType'"并得到错误:“类型'T'不符合协议'ProtoType'”
All insight and support greatly appreciated!非常感谢所有的见解和支持! Thank you!
谢谢!
Regarding issue #1:关于问题#1:
Instead of trying to create a generic function, I would create a remove function within each ViewModel (per model) individually.我不会尝试创建一个通用的 function,而是在每个 ViewModel(每个模型)中单独创建一个删除 function。 Additional to the issues you observe right now, you also have to tell firebase in which collection you want to remove the item:
除了您现在观察到的问题之外,您还必须告诉 firebase 您要在哪个集合中删除该项目:
db.collection ("collectionNameInFirebase") .document(documentID).delete() db.collection ("collectionNameInFirebase") .document(documentID).delete()
The follwoing code is coming from the firebase documentation, you see a defined collection (in this case "cities"), that is missing in you function call, even if you make the other stuff work, you would also have to add this information.以下代码来自 firebase 文档,您会看到定义的集合(在本例中为“城市”),在您的 function 调用中缺少该集合,即使您使其他内容正常工作,您也必须添加此信息。
private func deleteDocument() {
// [START delete_document]
db.collection("cities").document("DC").delete() { err in
if let err = err {
print("Error removing document: \(err)")
} else {
print("Document successfully removed!")
}
}
// [END delete_document]
}
Regarding issue #2:关于问题#2:
My classes start with creating constants to use firestore in the entire class.我的课程从创建常量开始,以在整个 class 中使用 firestore。 An example matching to the cities example from issue #1 would be:
与问题 #1 中的城市示例匹配的示例是:
import Swift
import Firebase
import FirebaseFirestore
import FirebaseFirestoreSwift
class CitiesViewModel: ObservableObject {
let db = Firestore.firestore()
let storage = Storage.storage()
// ... [a lot of other stuff] ...
private func deleteDocument(cityDocumentID: String) {
// [START delete_document]
db.collection("cities").document(cityDocumentID).delete() { err in
if let err = err {
print("Error removing document: \(err)")
} else {
print("Document successfully removed!")
}
}
// [END delete_document]
}
// ... [some more stuff] ...
}
Later you just can use db in the functions you create, identical to the example function I added under issue #1 (deleteDocument()).稍后您可以在您创建的函数中使用 db,这与我在问题 #1 (deleteDocument()) 下添加的示例 function 相同。
Best, Sebastian最好的,塞巴斯蒂安
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.