Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从getdocument中获取数组中的0个值,其中包含getdocuments_Ios_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Ios 从getdocument中获取数组中的0个值,其中包含getdocuments

Ios 从getdocument中获取数组中的0个值,其中包含getdocuments,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,我在firestore工作我有两个结构 一个叫历史 她叫了车 简单地说,他的理论结构是 struct UserOrderHistoryModel { var id : String? = "" var dateOrderCreated : String? = "" var cartArray : [CartItemModel] } struct CartItemModel { var id : String? = "" var restaurantIma

我在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中使用另一个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的函数中

    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)

                    }

                })



            }


        }
    }

}
我调试了代码,发现 一旦到达该函数的末尾,orderArray将返回到0值

这是我的火炉店的照片

图片的url:

更新: 我通过在第二个方法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)

                    }

                })



            }


        }
    }

}

firestore的图片?请添加您的数据库结构作为屏幕截图。请回复@AlexMamook我顺便解决了问题,但我展示了图片和解决方案,以帮助firestore的其他SPIC?请将您的数据库结构添加为屏幕截图。也请回复@AlexMamook我顺便解决了问题,但我会展示图片和解决方案以帮助他人