ios - 未调用调度组功能 - 如何使用调度组?

标签 ios swift firebase firebase-realtime-database

我有几个函数可以从 firebase 检索数据。我希望事件指示器一直旋转,直到检索到所有数据。

我的问题是,这些功能甚至都没有启动。为了搜索错误,我输入了未调用的打印语句。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    loadingActInd.hidesWhenStopped = true
    self.loadingActInd.startAnimating()      

    let group = dispatch_group_create()

    dispatch_group_enter(group)
    func loadTweetComplete() {
        dispatch_group_leave(group)
    }

    dispatch_group_enter(group)
    func loadEventsComplete() {
        dispatch_group_leave(group)
    }

    dispatch_group_notify(group, dispatch_get_main_queue()) {
        self.loadingActInd.stopAnimating()
        print("deejayTweetsDictionary = \(deejayTweetsDictionary)")
        print("finished executing")
    }

}

func loadTweetComplete(completionHandler: () -> ()) {

    print("TEST")

    deejayTweetsDictionary.removeAll()

    let usersRef = firebase.child("DeejayTweets").child(passedDJ.objectId)
    usersRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        if snapshot.exists() {

            deejayTweetsDictionary.removeAll()

            let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "date",ascending: false)])

            for element in sorted {

                deejayTweetsDictionary.append(element as! NSMutableDictionary)

            }

        }

        completionHandler()
    })
}

func loadEventsComplete(completionHandler: () -> ()) {

    print("TEST")

    eventDates.removeAll()

    if passedDJ.objectId != nil {

        let userRef = firebase.child("djBookings").child(passedDJ.objectId)

        userRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) in

            if snapshot.exists() {

                eventDates.removeAll()

                let item = (snapshot.value as? NSMutableDictionary)!

                let allValues = item.allValues
                for element in allValues {

                    eventDates.append(element as! NSMutableDictionary)

                }

            }

            completionHandler()
        })

    }
}

指标永远旋转,甚至 print("TEST") 语句都不会被调用。我究竟做错了什么?非常感谢您的帮助。

最佳答案

func loadTweetComplete() {
    dispatch_group_leave(group)
}

定义一个(嵌套)函数。您想要的是调用 以给定的完成处理程序作为参数的函数。 使用将是的尾随闭包语法

loadTweetComplete {
    dispatch_group_leave(group)
}

关于ios - 未调用调度组功能 - 如何使用调度组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39451274/

相关文章:

ios - 以编程方式在状态栏后面添加 View - swift

ios - 在 Swift 4 中使用掩码使 UIButton 文本透明

node.js - nodejs Firestore突然崩溃: Date objects

javascript - Firebase 在加载 View 之前未接收数据 - 在填充之前返回空数组

javascript - 如何使用 firestore 查询为 javascript 执行基于用户角色的登录

ios - 如何指定 uicollectionView 背景 View 的最小尺寸?

ios - 标题 Collection View 中的分组数据

ios - 过滤 TableView 时 UISearchField 出现问题

ScrollView 内的 SwiftUI 动画错误

ios - UIView removeFromSuperview 正在调用 didMoveToSuperview