Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Dispatch.main.async中的调度队列_Ios_Swift_Xcode_Grand Central Dispatch - Fatal编程技术网

Ios Dispatch.main.async中的调度队列

Ios Dispatch.main.async中的调度队列,ios,swift,xcode,grand-central-dispatch,Ios,Swift,Xcode,Grand Central Dispatch,我目前正在进行一个项目,该项目要求我更新数据库,然后在数据库上循环以比较一些值。由于数据库更新时间的关系,我决定使用延迟调用,给它时间进行更新。以下是我的通话结构: //Give database time to update DispatchQueue.main.asyncAfter(deadline: .now() +5) { let dispatchGroup = DispatchGroup() dispatchGroup.enter() closure {

我目前正在进行一个项目,该项目要求我更新数据库,然后在数据库上循环以比较一些值。由于数据库更新时间的关系,我决定使用延迟调用,给它时间进行更新。以下是我的通话结构:

//Give database time to update
DispatchQueue.main.asyncAfter(deadline: .now() +5) {
    let dispatchGroup = DispatchGroup()
    dispatchGroup.enter()

    closure {
        ...data processing....
        dispatchGroup.leave()
    }

    //wait for data-processing inside closure to complete
    dispatchGroup.wait()


}   

这冻结了我的应用程序。我的理解是,闭包应该异步运行,因此我希望在dispatchGroup.wait()中可以达到进入/离开平衡。如果您能帮助解决此问题,我们将不胜感激。谢谢

另请注意,在以下函数中,我偶尔会在我指示的行中出现EXC_断点错误:

- (void) fireEvent:(id <FEvent>)event queue:(dispatch_queue_t)queue {
    if ([event isCancelEvent]) {
    FCancelEvent *cancelEvent = event;
    FFLog(@"I-RDB065001", @"Raising cancel value event on %@", event.path);
    NSAssert(self.cancelCallback != nil, @"Raising a cancel event on a listener with no cancel callback");
    dispatch_async(queue, ^{
        self.cancelCallback(cancelEvent.error);
    });
} else if (self.callback != nil) {
    FDataEvent *dataEvent = event;
    FFLog(@"I-RDB065002", @"Raising value event on %@", dataEvent.snapshot.key);
    dispatch_async(queue, ^{
        self.callback(dataEvent.snapshot);  <---------
    });
}
-(void)fireEvent:(id)事件队列:(调度队列)队列{
如果([事件被取消]){
FCancelEvent*cancelEvent=事件;
FFLog(@“I-RDB065001”,@“在%@上提升取消值事件”,事件路径);
NSAssert(self.cancelCallback!=nil,@“在没有取消回调的侦听器上引发取消事件”);
调度异步(队列^{
self.cancelCallback(cancelEvent.error);
});
}else if(self.callback!=nil){
FDataEvent*dataEvent=event;
FFLog(@“I-RDB065002”,@“在%@上提升价值事件”,dataEvent.snapshot.key);
调度异步(队列^{

self.callback(dataEvent.snapshot);调度是异步的,但您正在主线程上运行它

您只需在后台线程上运行它,它不会阻塞UI

DispatchQueue.global(qos: .background).async {

}

“据我所知,闭包应该异步运行”但它是主线程。您不应该在主队列上运行长时间运行的任务,您是在主队列上以异步方式进行调度,因此会阻塞UI。构建任意时间延迟以允许执行异步任务是很脆弱的。您的数据库更新中应该有某种类型的完成处理程序,可以用于执行计算或发布通知,以便通知其他对象数据库更新已完成。添加到@Paulw11请注意,通知可以发布到同一线程的观察者。这很有意义,我最终使用了此解决方案,但改用了AsyncAfter。谢谢!