Ios 为什么GCD调度组notify未在操场和单元测试中调用

Ios 为什么GCD调度组notify未在操场和单元测试中调用,ios,swift,grand-central-dispatch,Ios,Swift,Grand Central Dispatch,我的代码在模拟器/设备中工作,我正在尝试为它编写单元测试。但是,在单元测试中不调用notify回调。这里有一个用于操场的代码,它也不调用notifycallback。我怀疑我可能使用了错误的队列,但无法确定应该使用哪个队列 import UIKit class Loader { func fetch(callback: ((_ result: String)-> Void)) { callback("SomeString") } } class My

我的代码在模拟器/设备中工作,我正在尝试为它编写单元测试。但是,在单元测试中不调用notify回调。这里有一个用于操场的代码,它也不调用notifycallback。我怀疑我可能使用了错误的队列,但无法确定应该使用哪个队列

import UIKit

class Loader {

    func fetch(callback: ((_ result: String)-> Void)) {

        callback("SomeString")
    }
}

class MyService {

   var list: Array<String> = Array()
   var loader: Loader = Loader()
   var dispatchGroup = DispatchGroup()

    func loadList(callback: @escaping (()-> Void)) {

       for i in 1...3 {

           self.dispatchGroup.enter()

           self.loader.fetch(callback: { [weak self] (string) in

               self?.list.append(string)
               self?.dispatchGroup.leave()
           })
       }

       dispatchGroup.notify(queue: .main) {

           callback()
       }
   }
}

var service = MyService()
service.loadList {

    print("Done is not called")
}
导入UIKit
类加载器{
func fetch(回调:((结果:字符串)->Void)){
回调(“SomeString”)
}
}
类MyService{
变量列表:Array=Array()
变量加载器:加载器=加载器()
var dispatchGroup=dispatchGroup()
func加载列表(回调:@escaping(()->Void)){
因为我在1…3{
self.dispatchGroup.enter()
self.loader.fetch(回调:{[weak self](字符串)在
self?.list.append(字符串)
self?.dispatchGroup.leave()
})
}
dispatchGroup.notify(队列:.main){
回调函数()
}
}
}
var service=MyService()
service.loadList{
打印(“未调用完成”)
}
更新

多亏了@paulvs,我们需要启用无限期执行。但是,如何为单元测试启用该功能

import UIKit
import PlaygroundSupport

class Loader {

    func fetch(callback: ((_ result: String)-> Void)) {

        callback("SomeString")
    }
}

class MyService {

    var list: Array<String> = Array()
    var loader: Loader = Loader()
    var dispatchGroup = DispatchGroup()

    func loadList(callback: @escaping (()-> Void)) {

        for i in 1...3 {

            self.dispatchGroup.enter()

            self.loader.fetch(callback: { [weak self] (string) in

                self?.list.append(string)
                self?.dispatchGroup.leave()
            })
        }

        dispatchGroup.notify(queue: .main) {

            callback()
        }
    }
}

PlaygroundPage.current.needsIndefiniteExecution = true

var service = MyService()
service.loadList {

    print("Done is called now!")
}
导入UIKit
导入PlaygroundSupport
类加载器{
func fetch(回调:((结果:字符串)->Void)){
回调(“SomeString”)
}
}
类MyService{
变量列表:Array=Array()
变量加载器:加载器=加载器()
var dispatchGroup=dispatchGroup()
func加载列表(回调:@escaping(()->Void)){
因为我在1…3{
self.dispatchGroup.enter()
self.loader.fetch(回调:{[weak self](字符串)在
self?.list.append(字符串)
self?.dispatchGroup.leave()
})
}
dispatchGroup.notify(队列:.main){
回调函数()
}
}
}
PlaygroundPage.current.NeedsDefiniteExecution=true
var service=MyService()
service.loadList{
打印(“现在调用完成!”)
}

感谢@paulvs的想法,下面是单元测试所需的代码:

let service = MyService()

let expect = expectation(description: "longRunningFunction")

service.loadList {

    expect.fulfill()
}

self.waitForExpectations(timeout: 0.5) { error in

    XCTAssert(service.isLoaded, "Not loaded")
}

可能重复。您是否尝试过启用(无限执行
XCPlaygroundPage.currentPage.needsIndefiniteExecu)‌​tion=true
在操场上?@paulvs谢谢,它在操场上起作用。你知道如何启用单元测试吗?
waitForExpectationsWithTimeout
应该在@Centurion.not
dispatchGroup.notifify()中起作用
必须在调度组完成之前调用?这正是我所拥有的,并且
组在单元测试中从未调用过notify
,但在我运行时它仍然会被调用。