ios - 当 firebase 没有任何东西可供抓取时,如何停止网络指示器以停止旋转

标签 ios swift function firebase

我有一个函数,在加载 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/

相关文章:

arrays - 根据对象属性的字符串数组过滤对象数组

c++ - C++ 中的 "Evaluate function"

c - getc() 作为宏和 C 标准库函数定义,连贯吗?

ios - iOS SDK 中集成的 Mixpanel 不发送事件

ios - SceneKit 节点不会改变场景根节点的位置

ios - 没有 Storyboard的 Swift 应用程序

objective-c - 以编程方式创建 UIView 和 UIViewController 并链接在一起的正确方法

swift - 如何在 GCD 中停止 DispatchWorkItem?

swift - 日期选择器,用户无法返回到之前的日期 Swift

javascript - 使用变量在 javascript 中调用特定函数而不是 IF 函数