多线程-工作组模式

来自:https://github.com/onevcat/Kingfisher

 

任务计数模式

 

    func testDownloadMultipleImages() {

        let expectation = self.expectation(description: "wait for all downloading finish")

        

        let group = DispatchGroup()

        

        for URLString in testKeys {

            if let url = URL(string: URLString) {

                group.enter()

                _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)

                downloader.downloadImage(with: url, options: nil, progressBlock: { (receivedSize, totalSize) -> Void in

                    

                }, completionHandler: { (image, error, imageURL, data) -> Void in

                    XCTAssert(image != nil, "Download should be able to finished for URL: \(String(describing: imageURL)).")

                    group.leave()

                })

            }

        }

        

        group.notify(queue: .main, execute: expectation.fulfill)

        waitForExpectations(timeout: 5, handler: nil)

    }

posted @ 2018-08-08 10:25  zzfx  阅读(159)  评论(0编辑  收藏  举报