【发布时间】:2022-01-15 00:08:29
【问题描述】:
我制作了一个 SwiftUI 应用程序,它通过多个 Firebase 云函数获取其 Firestore 数据。我想要获取的 Firestore 文档的结构如下:
现在,我想调用一个名为“getLocationObjectsFromUser”的云函数,它从用户相关集合locationIds 中获取所有LocationIds。然后,我想从具有特定 locationId 的位置文档中获取所有数据,包括集合“UserIds”。
我尝试过类似的方法,但在这种情况下,firebase 函数日志总是告诉我该函数已完成,尽管它还没有完成获取所有数据。正因为如此,我的 swift App 没有获得任何数据。我怎样才能返回我想要的所有数据?
功能代码:
exports.getLocationObjectsFromUser =
functions.https.onCall((data, context) => {
const locations = [];
let userIds = [];
return userRef
.doc(data.userId)
.collection("locationIds")
.where("status", "==", true)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
return locationRef
.doc(doc.id)
.get()
.then((locationDoc) => {
return locationRef
.doc(locationDoc.id)
.collection("userIds")
.get()
.then((querySnapshot1) => {
querySnapshot1.forEach((doc1) => {
userIds.push(doc1.id);
});
const object = {...locationDoc.data(), userIds};
locations.push(object);
userIds = [];
// if statement to avoid return before function has finished running
if (querySnapshot.size == locations.length) {
return {locations: locations};
}
});
});
});
});
});
Swift 代码:
func getLocationObjectsFromUser(_ user_id: String, onSuccess: @escaping ([LocationModel]) -> Void, onError: @escaping(_ error: String?) -> Void) {
let dic = ["userId" : user_id] as [String : Any]
self.functions.httpsCallable("getLocationObjectsFromUser").call(dic) { (result, error) in
if let error = error as NSError? {
print("ERROR")
print(error)
onError(error.localizedDescription)
}
if let data = result?.data as? [String: Any] {
print("DATA")
print(data)
// Later on i want to return the LocationModel with something like this: onSuccess(Data).
}
// i do not get any data after calling the function.
}
}
【问题讨论】:
标签: swift firebase asynchronous google-cloud-firestore google-cloud-functions