Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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/17.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 调度组通知似乎太早被呼叫_Ios_Swift_Asynchronous_Grand Central Dispatch_Nsoperation - Fatal编程技术网

Ios 调度组通知似乎太早被呼叫

Ios 调度组通知似乎太早被呼叫,ios,swift,asynchronous,grand-central-dispatch,nsoperation,Ios,Swift,Asynchronous,Grand Central Dispatch,Nsoperation,我正在尝试使用这里建议的调度组 但是,似乎在for循环的所有迭代完成之前调用了myGroup.notify。我做错了什么 let myGroup = DispatchGroup() for channel in channels.subscribedChannels() { myGroup.enter() buildUser(channel) { (success, user) in if success { addUser(user)

我正在尝试使用这里建议的调度组

但是,似乎在for循环的所有迭代完成之前调用了myGroup.notify。我做错了什么

let myGroup = DispatchGroup()

for channel in channels.subscribedChannels() {
    myGroup.enter()

    buildUser(channel) { (success, user) in
        if success {
            addUser(user)
        }

        print("Finished request \(user.id)")
        myGroup.leave()
    }
}

myGroup.notify(queue: .main) {
    print("Finished all requests.")
}
输出如下:

Finished request 1
Finished all requests.
Finished request 2

不确定,但您的
print(“Finished request\(user.id)”)
不是从线程调用的吗,因此可以在
print(“Finished all requests.”)之后调用它,因为它位于主优先级队列上

试着替换

打印(“已完成的请求\(user.id)”)
作者:

DispatchQueue.main.async{
打印(“已完成的请求\(user.id)”)
}
在操场上测试效果很好:

<代码>导入基础 导入PlaygroundSupport PlaygroundPage.current.NeedsDefiniteExecution=true 类用户{ 变量id:Int init(id:Int){ self.id=id } } 类频道{ var用户:用户 初始化(用户:用户){ self.user=用户 } } 变量subscribedChannels:[通道]=[] 让user1=User(id:1) 让user2=User(id:2) subscribedChannels.append(频道(用户:user1)) subscribedChannels.append(频道(用户:user2)) 让myGroup=DispatchGroup() 让bgQueue=DispatchQueue.global(qos:.后台) func doSomething(通道:通道,回调:@escaping(Bool,User)->Void){ 打印(“调用\(channel.user.id)”) bgQueue.asyncAfter(截止日期:.now()+1){ 回调(true,channel.user) } } 对于订阅频道中的频道{ myGroup.enter() doSomething(channel:channel){(success,user)在 如果成功{ // } 打印(“已完成的请求\(user.id)”) myGroup.leave() } } myGroup.notify(队列:.main){ 打印(“已完成所有请求”) }
这张照片

called for 1
called for 2
一秒钟后

Finished request 1
Finished request 2
Finished all requests.

我不知道你的类和方法,所以我很难知道更多的内容,但你的
print(“Finished request\(user.id)”)
不是从线程调用的吗,因此可以在
print(“Finished all requests.”)之后调用它,因为它位于主优先级队列上

试着替换

打印(“已完成的请求\(user.id)”)
作者:

DispatchQueue.main.async{
打印(“已完成的请求\(user.id)”)
}
在操场上测试效果很好:

<代码>导入基础 导入PlaygroundSupport PlaygroundPage.current.NeedsDefiniteExecution=true 类用户{ 变量id:Int init(id:Int){ self.id=id } } 类频道{ var用户:用户 初始化(用户:用户){ self.user=用户 } } 变量subscribedChannels:[通道]=[] 让user1=User(id:1) 让user2=User(id:2) subscribedChannels.append(频道(用户:user1)) subscribedChannels.append(频道(用户:user2)) 让myGroup=DispatchGroup() 让bgQueue=DispatchQueue.global(qos:.后台) func doSomething(通道:通道,回调:@escaping(Bool,User)->Void){ 打印(“调用\(channel.user.id)”) bgQueue.asyncAfter(截止日期:.now()+1){ 回调(true,channel.user) } } 对于订阅频道中的频道{ myGroup.enter() doSomething(channel:channel){(success,user)在 如果成功{ // } 打印(“已完成的请求\(user.id)”) myGroup.leave() } } myGroup.notify(队列:.main){ 打印(“已完成所有请求”) }
这张照片

called for 1
called for 2
一秒钟后

Finished request 1
Finished request 2
Finished all requests.

我不知道你的类和方法,所以我很难知道更多,如果你把
print(“输入请求”)
放在
myGroup.enter()之前,你会得到什么?如果你把
print(“输入请求”)
放在
myGroup.enter()之前,你会得到什么?