我突然从从Firebase下载用户目录的函数中得到了一些奇怪的结果。在控制台打印中,我看到该函数打印了5次,这意味着该函数正在循环播放。我正在从Firebase Storage异步下载产品图片。 我首先登录时以级联调用的方式调用此函数,以获取用户的详细信息,订单,销售等。我想到此选项是因为用户需要更换iPad。
您能看到函数在哪里循环5次吗?
这是功能:
javax.servlet.http.HttpServletRequest
这是级联调用:
static func downloadProducts(completed: @escaping (Bool) -> (), vendor: String) {
print("Firebase.downloadProducts() : Query for products in Firebase started")
let ref = Database.database().reference()
ref.child("Continent").child("Europe").child("Country").child("\(UserDetails.country!)").child("Catalog").queryOrdered(byChild: "Product Vendor").queryEqual(toValue: String(describing: UserDetails.fullName!)).observeSingleEvent(of: .value) { (snapshot) in
print("downloadProducts() : snapshot is: \(snapshot)")
guard let data = snapshot.value as? [String: [String : String]] else { print("downloadProducts() : data is not [String: [String : String]] "); return}
// guard let data = snapshot.value as? [String : String] else { print("downloadProducts() : wrong type of data"); return}
for (_, value) in data {
let productId = value["Product Id"]!
let vendor = value["Product Vendor"]!
let availableQuantity = value["Available Quantity"]!
let soldQuantity = value["Sold Quantity"]!
let minimumStock = value["Minimum Stock"]!
let name = value["Product Name"]!
let price = value["Product Price"]!
let category = value["Product Category"]!
let description = value["Product Description"]!
let pictureUrl = value["Product Picture Url"]!
let url = URL(string: pictureUrl)!
DispatchQueue.global().async {
let data = try? Data(contentsOf : url)
DispatchQueue.main.async {
let productImage = UIImage(data: data!)!
do{
try Product.saveProduct(completed: { (true) in
print("Firebase.downloadProducts() : Product from Firebase added to CoreData")
}, productImage: productImage, productId: productId, category: category, name: name, price: price, vendor: vendor, availableQuantity: availableQuantity, soldQuantity: soldQuantity, minimumStock: minimumStock, description: description)
} catch {
print("Firebase.downloadProducts() : Error saving product to CoreData: \(error)")
}
}
print("downloadProducts() : User inventory found in Firebase and saved to CoreData")
print("downloadProducts() : Query for products in Firebase ended")
completed(true)
}
}
}
}
答案 0 :(得分:0)
您在这里多次致电完成
DispatchQueue.global().async {
let data = try? Data(contentsOf : url)
DispatchQueue.main.async {
let productImage = UIImage(data: data!)!
do{
try Product.saveProduct(completed: { (true) in
print("Firebase.downloadProducts() : Product from Firebase added to CoreData")
}, productImage: productImage, productId: productId, category: category, name: name, price: price, vendor: vendor, availableQuantity: availableQuantity, soldQuantity: soldQuantity, minimumStock: minimumStock, description: description)
} catch {
print("Firebase.downloadProducts() : Error saving product to CoreData: \(error)")
}
}
print("downloadProducts() : User inventory found in Firebase and saved to CoreData")
print("downloadProducts() : Query for products in Firebase ended")
completed(true)
}
但您应该
let g = DispatchGroup()
for (_, value) in data {
...
...
...
DispatchQueue.global().async {
g.enter()
let data = try? Data(contentsOf : url)
DispatchQueue.main.async {
let productImage = UIImage(data: data!)!
do{
try Product.saveProduct(completed: { (true) in
print("Firebase.downloadProducts() : Product from Firebase added to CoreData")
}, productImage: productImage, productId: productId, category: category, name: name, price: price, vendor: vendor, availableQuantity: availableQuantity, soldQuantity: soldQuantity, minimumStock: minimumStock, description: description)
} catch {
print("Firebase.downloadProducts() : Error saving product to CoreData: \(error)")
}
}
print("downloadProducts() : User inventory found in Firebase and saved to CoreData")
print("downloadProducts() : Query for products in Firebase ended")
g.leave()
}
}
g.notify(queue:.main) {
completed(true)
}
答案 1 :(得分:0)
我终于找到了问题所在。在Firebase.downloadOrders()
中,我打电话给complete(true)
是为了将订单保存到Core Data,因此对于保存的每个订单,都会调用Firebase.downloadProduct()。将其放在for in循环之后,现在可以正确下载产品。