I'm a relatively new coder and have this fetch method that works fine. I want to adapt it so that I can call a fetch for other collections (and their respective Structs) so I don't have to keep re-creating this same block of code with only very minor changes for each different type of data call.
I know I need to expand my parameters to being :
func fetchMachines2 (_ userID:String, _ collection:String, _initialization:whatDataTypeDoiUse?)
I don't know how to do that last part. Using this example, how do I create/define mData outside of the method so that I can pass it in?
//rework this to accept collection, the datainit, and the
func fetchMachines2 (_ userID: String) {
var machines = [Machine]()
db.collection("machines").whereField("userID", isEqualTo: userID).getDocuments { (snapshot, error) in
if error == nil && snapshot != nil {
for doc in snapshot!.documents {
let mData = Machine(
imgURL: doc["imgURL"] as? String,
machineID: doc["machineID"] as? String,
name: doc["name"] as? String,
type: doc["type"] as? String,
userID: doc["userID"] as? String
)
machines.append(mData)
}
self.delegateMFP?.receiveMachines(machines: machines)
}
else {
print ("there is some error with the get documents")
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…