我有一个函数,在加载 View 时以及用户下拉 TableView 以刷新 View 时调用。该函数拍摄快照,然后将信息放在 tableView 上。我有一个正在使用的网络事件指示器。当有信息可供抓取时,它可以正常工作,并且一旦获取数据,He 指示器就会停止旋转,但如果没有数据可供抓取,那么它就会继续旋转。如果没有数据可供抓取,我想让它停止旋转。
ref.observe(.childAdded, with: { (snapshot) in
let userId = snapshot.key
print(snapshot.childrenCount)
//print("This is interesting...", snapshot.value)
print(userId)
if userId == uid {
let bookRef = FIRDatabase.database().reference().child("user-books").child(userId)
bookRef.observe(.childAdded, with: { (snapshot) in
let bookID = snapshot.key
print("This is new",snapshot.children)
print(bookID,"sigh..")
self.bookKey = snapshot.key
let booksIDref = FIRDatabase.database().reference().child("books").child(bookID)
booksIDref.observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
let book = Book()
book.setValuesForKeys(dictionary)
self.books.append(book)
print(book.Author)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}, withCancel: nil)
}, withCancel: nil) }
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}, withCancel: nil)
}
最佳答案
在 FrankvanPuffelen 的帮助下,我找到了正确的方法。我使用了 snapshot.exists ()
我把它放在第二个快照中,因为如果那里没有存储任何项目,那么第二个快照将为空。我使用 if 语句作为检查,现在效果非常好。
ref.observe(.childAdded, with: { (snapshot) in
let userId = snapshot.key
print(snapshot.childrenCount)
print(userId)
if userId == uid {
let bookRef = FIRDatabase.database().reference().child("user-books").child(userId)
bookRef.observe(.childAdded, with: { (snapshot) in
let bookID = snapshot.key
print("This is new",snapshot.children)
self.bookKey = snapshot.key
let booksIDref = FIRDatabase.database().reference().child("books").child(bookID)
booksIDref.observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
let book = Book()
book.setValuesForKeys(dictionary)
self.books.append(book)
print(book.Author)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}, withCancel: nil)
if snapshot.exists(){
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
}, withCancel: nil) }
}, withCancel: nil)
关于ios - 当 firebase 没有任何东西可供抓取时,如何停止网络指示器以停止旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740098/