Activity 指示器不会完成操作
Activity indicator won't finish action
我有一个 activity 指示器,一旦从 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()
}
.childAdded
事件仅在出现 child 时触发。因此,正如您所指出的:如果可能没有 children,则不能使用 .childAdded
来隐藏 activity 指标。
唯一保证触发的事件是 .value
,因此您需要使用它来检测条件:
let ref = self.ref?.child("Users").child(currentUserID).child("Products")
ref.observe(.value, with:
{ (snapshot) in
self.activityView.stopAnimating()
})
如果您这样做,无论如何您都会在此处理程序中获得 child 节点。所以你也可以考虑遍历 snapshot
的 child 节点并在这里处理它们:
ref.observe(.value, with:
{ (snapshot) in
self.activityView.stopAnimating()
for child in snapshot.children.allObjects as [FIRDataSnapshot] {
...
}
})
我有一个 activity 指示器,一旦从 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()
}
.childAdded
事件仅在出现 child 时触发。因此,正如您所指出的:如果可能没有 children,则不能使用 .childAdded
来隐藏 activity 指标。
唯一保证触发的事件是 .value
,因此您需要使用它来检测条件:
let ref = self.ref?.child("Users").child(currentUserID).child("Products")
ref.observe(.value, with:
{ (snapshot) in
self.activityView.stopAnimating()
})
如果您这样做,无论如何您都会在此处理程序中获得 child 节点。所以你也可以考虑遍历 snapshot
的 child 节点并在这里处理它们:
ref.observe(.value, with:
{ (snapshot) in
self.activityView.stopAnimating()
for child in snapshot.children.allObjects as [FIRDataSnapshot] {
...
}
})