Swift(iOS),在返回

时间:2016-01-23 17:24:42

标签: ios swift alamofire

我正在编写一个Swift iOS应用程序(我的第一个,所以请耐心等待我),我使用Swifter HTTP服务器来处理各种请求。一个这样的请求是带有JSON数组的HTTP POST,指定要从Web下载的图像(并做一些其他的事情,与手头的问题无关)。

我使用Alamofire下载图像(这很好用),但我正在寻找好的(最好是简单的)方法来等待所有图像完成下载,然后再回复上面的POST请求的响应(因为响应有包含指示结果的JSON,包括任何失败的下载。)

实现这一目标的好方法是什么(最好不要阻止主线程)?

以下是一些要说明的片段:

public func webServer(publicDir: String?) -> HttpServer {
  let server = HttpServer()

  server.POST["/images/update"] = { r in
        let images = ...(from JSON array in body)
        let updateResult = ImageUtil.updateImages(images)
        let resultJson: String = Mapper().toJSONString(updateResult, prettyPrint: true)!

        if updateResult.success {
            return .OK(.Text(resultJson))
        }
        return HttpResponse.RAW(500, "Error", nil, { $0.write([UInt8](updateResult.errorMessage.utf8)) })
    }
}

static func updateImages(images: [ImageInfo]) -> UpdateResult {
  let updateResult = UpdateResult()
  for image in images {
    Alamofire.download(.GET, serverFile.imageUrl) { temporaryURL, response in return destinationPath }
        .validate()
        .response{_, _, _, error in
            if let error = error {
                Log.error?.message("Error downloading file \(image.imageUrl) to \(image.fileName): \(error)")
            } else {
                updateResult.filesDownloaded++
                Log.info?.message("Downloaded file \(image.imageUrl) to \(image.fileName)")
            }}
    }

    return updateResult // It obviously returns before any images finish downloading.  I need to wait until all images have downloaded before I can return an accurate result.
}

2016年1月23日更新,每个bbum使用调度程序

这是尝试使用调度程序机制,但是对updateImages的调用仍会立即返回(即使使用dispatch_sync时)。

在将HTTP响应返回给调用方之前,如何等待所有下载完成?

public func webServer(publicDir: String?) -> HttpServer {
    let server = HttpServer()

    server.POST["/images/update"] = { r in
        let imageDownloader = ImageDownloader()
        imageDownloader.updateimageFiles(adFilesOnServer)
        let resultJson: String = Mapper().toJSONString(imageDownloader.updateResult, prettyPrint: true)!

        if imageDownloader.updateResult.success {
            return .OK(.Text(resultJson))
        }
        return HttpResponse.RAW(500, "Error", nil, { $0.write([UInt8](imageDownloader.updateResult.errorMessage.utf8)) })
    }
}

class ImageDownloader {

    var updateResult = AdUpdateResult()
    private var imageFilesOnServer = [ImageFile]()

    private let fileManager = NSFileManager.defaultManager()
    private let imageDirectoryURL = NSURL(fileURLWithPath: Settings.imageDirectory, isDirectory: true)

    private let semaphore = dispatch_semaphore_create(4)
    private let downloadQueue = dispatch_queue_create("com.acme.downloader", DISPATCH_QUEUE_SERIAL)

    func updateimageFiles(imageFilesOnServer: [ImageFile]) {
        self.imageFilesOnServer = imageFilesOnServer

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)

        for serverFile in imageFilesOnServer {
            downloadImageFileFromServer(serverFile)
        }

        dispatch_sync(downloadQueue) {
            dispatch_sync(dispatch_get_main_queue()) {
                print("done") // It gets here before images have downloaded.
            }
        }
    }

    private func downloadImageFileFromServer(serverFile: ImageFile) {

        let destinationPath = imageDirectoryURL.URLByAppendingPathComponent(serverFile.fileName)

        Alamofire.download(.GET, serverFile.imageUrl) { temporaryURL, response in return destinationPath }
        .validate()
        .response { _, _, _, error in
            if let error = error {
                Log.error?.message("Error downloading file \(serverFile.imageUrl) to \(serverFile.fileName): \(error)")
            } else {
                self.updateResult.filesDownloaded++
                Log.info?.message("Downloaded file \(serverFile.imageUrl) to \(serverFile.fileName)")
            }
            dispatch_semaphore_signal(self.semaphore)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,你真的不希望在没有某种节流的情况下触发每张图像的请求。信号量对于那种事情很有效。

其次,您需要基本计算未完成的操作数,然后在完成所有操作后触发完成处理程序。或者,如果可以随时启动新操作,您可能希望对操作进行分组。

所以,伪代码:

sema = dispatch_semaphore_create(4) // 4 being # of concurrent operations allowed
serialQ = dispatch_queue_create(.., SERIAL)

dispatch_async(serialQ) {
    dispatch_semaphore_wait(sema, FOREVER) // will block if there are 4 in flight already
    for image in images {
        downloader.downloadAsync(image, ...) { // completion
              dispatch_semaphore_signal(sema) // signal that we are done with one
              ... handle downloaded image or error ...
              ... add downloaded images to downloadedImages ...
        }
    }
}

dispatch_async(serialQ) {
     // since serialQ is serial, this will be executed after the downloads are done
    dispatch_async(main_queue()) {
         yo_main_queue_here_be_yer_images(... downloadedImages ...)
    }
}