我与Firestore合作我有两个结构 一个叫做历史 另一个叫购物车 只是历史结构是
struct UserOrderHistoryModel {
var id : String? = ""
var dateOrderCreated : String? = ""
var cartArray : [CartItemModel]
}
struct CartItemModel {
var id : String? = ""
var restaurantImageUrl : String
var restaurantTitle : String
var menuItemImageUrl : String
var menuItemTitle : String
var countOfMenuItemSelected : Int
}
我要执行的操作是加载历史记录,因此我使用getdocument获取ID和日期
我想获取历史记录文件中的订单集合 所以我在第一个里面使用了另一个getdocuments
func loadUserOrderHistory(completion : @escaping (_ error :Error? ,_ userHistoryArray : [UserOrderHistoryModel]?) -> ()) {
var historyArray = [UserOrderHistoryModel]()
let userHistoryRef = USERS.document(UserConfigurations.currentUserID!).collection("history")
userHistoryRef.getDocuments { (snapShots, error) in
if error != nil {
completion(error , nil)
}else {
historyArray.removeAll()
for document in snapShots!.documents {
let historyId = document.documentID
let historyData = document.data()
let historyDate = historyData["date_order_created"] as? Timestamp ?? nil
let historyDateToString = String(describing: historyDate?.dateValue())
var orderArray = [CartItemModel]()
self.loadUserHistoryOrders(histroyDocumentReference: userHistoryRef.document(historyId), completion: { (error, cartArray) in
if error != nil {
print(error!)
}else {
orderArray = cartArray!
}
})
let userHistory = UserOrderHistoryModel(id: historyId, dateOrderCreated: historyDateToString , cartArray: self.orderArray)
historyArray.append(userHistory)
}
completion(nil , historyArray)
}
}
}
private func loadUserHistoryOrders( histroyDocumentReference : DocumentReference, completion : @escaping (_ error : Error? ,_ historyOrders : [CartItemModel]? ) -> ()) {
var cartArray = [CartItemModel]()
histroyDocumentReference.collection("orders").getDocuments { (snapShot, error) in
if error != nil {
completion(error,nil)
}else {
for document in snapShot!.documents {
let id = document.documentID
let cartDictionary = document.data()
let restaurantImageUrl = cartDictionary["restaurant_imageurl"] as? String ?? "none"
let restaurantName = cartDictionary["restaurant_title"] as? String ?? "none"
let menuItemImageUrl = cartDictionary["menuItem_imageurl"] as? String ?? "none"
let menuItemName = cartDictionary["menuItem_title"] as? String ?? "none"
let count = cartDictionary["number_of_selected_menuitem"] as? Int ?? 0
let cart = CartItemModel(id: id, restaurantImageUrl: restaurantImageUrl, restaurantTitle: restaurantName, menuItemImageUrl: menuItemImageUrl, menuItemTitle: menuItemName, countOfMenuItemSelected: count)
cartArray.append(cart)
}
completion(nil , cartArray)
}
}
}
第二个getdocumnet中的orderArray,我将它放在另一个名为loadUserHistoryOrders的函数中
我调试了代码,发现 一旦我到达该函数的末尾,orderArray将返回到0值
这是我的消防站的照片
图片网址:https://drive.google.com/open?id=1NX8NIUN2Yb9m3_7A8EnZgAxWe8xuhNDh
更新: 我通过在第二个方法loadUserHistoryOrders的调用内添加loadUserOrderHistory完成来解决了这个问题
func loadUserOrderHistory(completion : @escaping (_ error :Error? ,_ userHistoryArray : [UserOrderHistoryModel]?) -> ()) {
var historyArray = [UserOrderHistoryModel]()
let userHistoryRef = USERS.document(UserConfigurations.currentUserID!).collection("history")
userHistoryRef.getDocuments { (snapShots, error) in
if error != nil {
completion(error , nil)
}else {
historyArray.removeAll()
for document in snapShots!.documents {
let historyId = document.documentID
let historyData = document.data()
let historyDate = historyData["date_order_created"] as? Timestamp ?? nil
let historyDateToString = String(describing: historyDate?.dateValue())
var orderArray = [CartItemModel]()
self.loadUserHistoryOrders(histroyDocumentReference: userHistoryRef.document(historyId), completion: { (error, cartArray) in
if error != nil {
print(error!)
}else {
orderArray = cartArray!
let userHistory = UserOrderHistoryModel(id: historyId, dateOrderCreated: historyDateToString , cartArray: self.orderArray)
historyArray.append(userHistory)
completion(nil , historyArray)
}
})
}
}
}
}