简体   繁体   中英

Function loops when downloading from Firebase Swift

I'm suddenly getting some weird results from a function that downloads user's catalog from Firebase. In console prints I see the function prints 5 times, meaning the function is looping. I'm downloading the product picture from Firebase Storage asynchronously. I call this function in a cascade calling at first log in, in order to get back user's details, orders, sales and so on. I thought of this option for user's need to change iPad.

Can you see where the function gets looping 5 times?

This is the function :

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)
                }
            }
        }
    }

and this is the cascade calling :

static func findUser(userName: String)  {
        print("findUser(): checkinf for User in CoreData")
        let context = CoreData.databaseContext
        let request: NSFetchRequest<User> = User.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: false),NSSortDescriptor(key: "logo", ascending: false)]
        request.predicate = NSPredicate(format: "name == %@", userName)
        do {
            let fetch = try context.fetch(request)
            if fetch.count > 0 {
                print(" findUser() : User is a returning user")
                for value in fetch { // user exists
                    UserDetails.shopId = value.shopId
                    UserDetails.fullName = value.name
                    UserDetails.logo = UIImage(data: value.logo! as Data )
                    UserDetails.logoUrl = value.logoUrl
                    UserDetails.address = value.address
                    UserDetails.zipCode = value.zipCode
                    UserDetails.city = value.city
                    UserDetails.region = value.region
                    UserDetails.country = value.country
                    UserDetails.phoneNumber = value.phoneNumber
                    UserDetails.latitude = value.latidute
                    UserDetails.longitude = value.longitude
                }
            } else {
                print(" findUser(): User is a 1st time user in device, will now check for user in Firebase")
                Firebase.downloadShopDetails(completed: { (true) in
                    if UserDetails.shopId == nil {
                        // user is not registered in Firebase
                        print("findUser() : User not found in Firebase, user is First time user ")
                    } else {
                        // user has been found in Firebase
                        print("findUser() : user found in Firebase, will now check for opening times in Firebase")
                        Firebase.downloadOpeningTimes(completed: { (true) in

                            print("findUser() : Opening times downloaded from Firebase, will now check for user orders in Firebase")
                            Firebase.downloadOrders(completed: { (true) in

                                print("findUser() : user orders downloaded from Firebase, will now check for user products in Firebase")
                                Firebase.downloadProducts(completed: { (true) in
                                    print("findUser() : Inventoy downloaded from Firebase")
                                }, vendor: UserDetails.fullName!)

                            })

                        }, shopName: UserDetails.fullName!)

                    }
                }, shopName: userName)
                UserDetails.shopId = UUID().uuidString
                UserDetails.fullName = userName
            }
        } catch  {
            print("findUser(): Error loading user details : \(error)")
        }
    }

You call the completion here many times

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)
}

but you should

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)
}

I finally found where the problem was.In Firebase.downloadOrders() I was calling complete(true) at saving orders to Core Data, hence for every order saved the `Firebase.downloadProduct() was called. Placed it after the for in loop and now products are downloaded properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM