活动指标未能完成操作

时间:2018-02-24 13:34:04

标签: swift firebase firebase-realtime-database uicollectionview uiactivityindicatorview

我有一个活动指示器,一旦从Firebase读取完毕就会停止。

如果我的用户添加了产品,指标就会完好无损。唉,如果没有上传任何产品的用户试图看到他的,那么该指标就不会完成其行动。

我的代码:

override func viewDidLoad()
{

    AddProductImage.image = #imageLiteral(resourceName: "PLUS")

    let singlePlusTap = UITapGestureRecognizer(target: self, action: Selector("AddProductTapDetected"))
    AddProductImage.isUserInteractionEnabled = true
    AddProductImage.addGestureRecognizer(singlePlusTap)
    // Do any additional setup after loading the view.
    self.MyProductsCollection.delegate = self
    self.MyProductsCollection.dataSource = self;


    // create a hover view that covers all screen with opacity 0.4 to show a waiting action
    let fadeView:UIView = UIView()
    fadeView.frame = self.view.frame
    fadeView.backgroundColor = UIColor.white
    fadeView.alpha = 0.4

    // add fade view to main view
    self.view.addSubview(fadeView)
    // add activity to main view
    self.view.addSubview(activityView)
    activityView.hidesWhenStopped = true
    activityView.center = self.view.center
    // start animating activity view
    activityView.startAnimating()

    self.ref = Database.database().reference()
    let loggedOnUserID = Auth.auth().currentUser?.uid


    if let currentUserID = loggedOnUserID
    {
        // Retrieve the products and listen for changes
         self.ref?.child("Users").child(currentUserID).child("Products").observe(.childAdded, with:
            { (snapshot) in

                // Code to execute when new product is added
                let prodValue = snapshot.value as? NSDictionary
                let prodName = prodValue?["Name"] as? String ?? ""
                let prodPrice = prodValue?["Price"] as? Double ?? -1
                let prodDesc = prodValue?["Description"] as? String ?? ""
                let prodURLS = prodValue?["MainImage"] as? String
                let prodAmount = prodValue?["Amount"] as? Int ?? 0
                let prodID = snapshot.key

                let prodToAddToView = Product(name: prodName, price: prodPrice, currency: "NIS", description: prodDesc, location: "IL",
                                              toSell: false, toBuy: false, owner: currentUserID, uniqueID: prodID, amount: prodAmount, mainImageURL: prodURLS)


                self.products.append(prodToAddToView)
                DispatchQueue.main.async
                    {
                        self.MyProductsCollection.reloadData()
                        // remove the hover view as now we have data
                        fadeView.removeFromSuperview()
                        // stop animating the activity
                        self.activityView.stopAnimating()
                }
        }
        ) // Closes observe function
    }
    super.viewDidLoad()

}

1 个答案:

答案 0 :(得分:1)

.childAdded事件仅在有孩子时触发。因此,您注意到:如果可能没有孩子,则无法使用.childAdded隐藏活动指示符。

唯一可以保证触发的事件是.value,因此您需要使用它来检测条件:

let ref = self.ref?.child("Users").child(currentUserID).child("Products")
ref.observe(.value, with:
        { (snapshot) in
    self.activityView.stopAnimating()
})

如果这样做,无论如何都会在此处理程序中获取子节点。因此,您也可以考虑循环遍历snapshot的子节点并在此处理它们:

ref.observe(.value, with:
        { (snapshot) in
    self.activityView.stopAnimating()
    for child in snapshot.children.allObjects as [FIRDataSnapshot] {
       ...
    }
})