Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift 在OperationQueue don'中打印1到10;不要打印整数_Swift_Multithreading_Combine_Operationqueue - Fatal编程技术网

Swift 在OperationQueue don'中打印1到10;不要打印整数

Swift 在OperationQueue don'中打印1到10;不要打印整数,swift,multithreading,combine,operationqueue,Swift,Multithreading,Combine,Operationqueue,我正在学习联合调度程序,我有来自Raywenderlich书的示例代码 let queue = OperationQueue() let subscription = (1...10).publisher .receive(on: queue) .sink { value in print("Received \(value) on thread \(Thread.current.number)") } 这本书解释说OperationQueue使用所

我正在学习联合调度程序,我有来自Raywenderlich书的示例代码

let queue = OperationQueue()

let subscription = (1...10).publisher
  .receive(on: queue)
  .sink { value in
    print("Received \(value) on thread \(Thread.current.number)")
  }

这本书解释说OperationQueue使用所有可用线程,因此打印顺序和线程可能是随机的。我理解这一部分,但当我在操场上运行此代码时,我只看到10个数字中的5个

Received 1 on thread 7
Received 2 on thread 6
Received 3 on thread 5
Received 7 on thread 9
Received 6 on thread 4

为什么该代码没有显示所有10个数字???

您需要导入
PlaygroundSupport
并在文件顶部设置
PlaygroundPage.current.needsDefiniteExecution=true

如果使用另一个sink方法,也就是为其获取完成处理程序块的方法,则应在该完成处理程序中调用
PlaygroundPage.current.finishExecution()

编辑:在队列上的屏障块中调用
PlaygroundPage.current.finishExecution()
,否则将在所有打印语句执行之前调用它

以下是我的测试代码:

PlaygroundPage.current.needsIndefiniteExecution = true

let queue = OperationQueue()

let subscription = (1...10).publisher
    .receive(on: queue)
    .sink(receiveCompletion: { _ in
        queue.addBarrierBlock {
            print("Finished")
            PlaygroundPage.current.finishExecution()
        }
    }, receiveValue: { value in
        print("Received \(value) on thread \(Thread.current)")
    })
以及输出:

Received 1 on thread <NSThread: 0x7fd86f204080>{number = 2, name = (null)}
Received 2 on thread <NSThread: 0x7fd86f404340>{number = 3, name = (null)}
Received 3 on thread <NSThread: 0x7fd86f404430>{number = 4, name = (null)}
Received 7 on thread <NSThread: 0x7fd86f405240>{number = 5, name = (null)}
Received 6 on thread <NSThread: 0x7fd86f205c50>{number = 6, name = (null)}
Received 10 on thread <NSThread: 0x7fd86ce487f0>{number = 7, name = (null)}
Received 5 on thread <NSThread: 0x7fd86cf048a0>{number = 10, name = (null)}
Received 9 on thread <NSThread: 0x7fd86ce49b20>{number = 11, name = (null)}
Received 4 on thread <NSThread: 0x7fd86cf2a4c0>{number = 12, name = (null)}
Received 8 on thread <NSThread: 0x7fd86ce4ad60>{number = 13, name = (null)}
Finished
在线程{number=2,name=(null)}上收到1
在线程{number=3,name=(null)}上接收到2
在线程{number=4,name=(null)}上收到3
在线程{number=5,name=(null)}上收到7
在线程{number=6,name=(null)}上收到6
在线程{number=7,name=(null)}上收到10
在线程{number=10,name=(null)}上收到5
在线程{number=11,name=(null)}上收到9
在线程{number=12,name=(null)}上收到4
在线程{number=13,name=(null)}上收到8
完成了

我在将该值更改为true后进行了测试,但它打印了相同的结果。