Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Swift 需要在一系列getDocument价格查找之后获取购物车金额_Swift_Google Cloud Platform_Google Cloud Firestore - Fatal编程技术网

Swift 需要在一系列getDocument价格查找之后获取购物车金额

Swift 需要在一系列getDocument价格查找之后获取购物车金额,swift,google-cloud-platform,google-cloud-firestore,Swift,Google Cloud Platform,Google Cloud Firestore,我试图通过执行一系列for循环调用来计算购物车中的物品总数,从而获得Firestore中购物车的总数 购物车存储在一个名为carts的表中,视图一出现就会被拉入一个名为cartCache的应用程序内字典中,类型为[String:Int]?其中,键是项目的UID,值是项目的数量。但是,项目的价格存储在另一个名为项目的集合中。我只想在视图出现或消失时计算总和,然后将其显示在UILabel中 因此,为了获得总额并向用户报告(通过修改标签或将其写入购物车),我需要迭代购物车中的每个项目,并通过API调用

我试图通过执行一系列for循环调用来计算购物车中的物品总数,从而获得Firestore中购物车的总数

购物车存储在一个名为carts的表中,视图一出现就会被拉入一个名为cartCache的应用程序内字典中,类型为[String:Int]?其中,键是项目的UID,值是项目的数量。但是,项目的价格存储在另一个名为项目的集合中。我只想在视图出现或消失时计算总和,然后将其显示在UILabel中

因此,为了获得总额并向用户报告(通过修改标签或将其写入购物车),我需要迭代购物车中的每个项目,并通过API调用getDocument(completion:)获取价格。然而,这意味着总和的计算是异步的,这不一定是个问题,但我不确定如何为for循环提供一个完成处理程序。有没有更简单的方法

for item in self.cartCache!.keys
      {
        let doc = self.itemCollectionRef.document(item).getDocument
        {
          (itemDocument, error) in
          {
            guard let itemDocument = itemDocument, itemDocument.exists else {return}
            sum += itemDocument.data()!["price"] as! Int
          }
        }
      }

//Do something with sum (no guarantee at this point that sum is actually calculated, how do I add
//a completion handler to a for loop or obtain multiple field's values at once?

您需要使用的是
调度组
DispatchGroup
s允许您一次运行多个异步函数,并且在每个异步函数完成后调用一个“完成处理程序”。一些基本代码如下所示:

//创建您的调度组。这将管理所有异步任务,并在它们完成时运行一些代码
let group=DispatchGroup()
对于数组中的x{
//在异步函数之前调用enter
group.enter()
ASNYNCHRONOUS功能(完成:{
//异步函数完成后调用leave
小组请假()
})
}
通知组(队列:.main,执行:{
//在group.notify内部,在完成所有异步函数后运行您想要运行的任何代码。
//group.notify是您的“完成处理程序”
})
那么,以你的例子来说,它看起来是这样的:

let group=DispatchGroup()
对于self.cartCache!中的项!。钥匙
{
group.enter()
让doc=self.itemCollectionRef.document(item).getDocument
{
(项目文档,错误)在
{
guard let itemDocument=itemDocument,itemDocument.exists else{return}
sum+=itemDocument.data()![“price”]as!Int
小组请假()
}
}
}
通知组(队列:.main,执行:{
//您的所有异步函数都已完成。您可以继续了
})
来源:


更深入:

您需要使用的是
DispatchGroup
DispatchGroup
s允许您一次运行多个异步函数,并且在每个异步函数完成后调用一个“完成处理程序”。一些基本代码如下所示:

//创建您的调度组。这将管理所有异步任务,并在它们完成时运行一些代码
let group=DispatchGroup()
对于数组中的x{
//在异步函数之前调用enter
group.enter()
ASNYNCHRONOUS功能(完成:{
//异步函数完成后调用leave
小组请假()
})
}
通知组(队列:.main,执行:{
//在group.notify内部,在完成所有异步函数后运行您想要运行的任何代码。
//group.notify是您的“完成处理程序”
})
那么,以你的例子来说,它看起来是这样的:

let group=DispatchGroup()
对于self.cartCache!中的项!。钥匙
{
group.enter()
让doc=self.itemCollectionRef.document(item).getDocument
{
(项目文档,错误)在
{
guard let itemDocument=itemDocument,itemDocument.exists else{return}
sum+=itemDocument.data()![“price”]as!Int
小组请假()
}
}
}
通知组(队列:.main,执行:{
//您的所有异步函数都已完成。您可以继续了
})
来源:

更深入: