我有一个函数,在加载视图时以及当用户下拉表视图以刷新视图时调用。此函数拍摄快照,然后将信息放在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)
}
答案 0 :(得分:0)
我找到了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)