Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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 如何使用ReactiveCocoa为每个下载的文件仅订阅一次多个选择器_Ios_Swift_Nsurlsession_Reactive Cocoa - Fatal编程技术网

Ios 如何使用ReactiveCocoa为每个下载的文件仅订阅一次多个选择器

Ios 如何使用ReactiveCocoa为每个下载的文件仅订阅一次多个选择器,ios,swift,nsurlsession,reactive-cocoa,Ios,Swift,Nsurlsession,Reactive Cocoa,我不熟悉ReactiveCocoa,并尝试使用它构建API客户机。我希望RAC观察员在每个URLSession:downloadTask:didFinishdownloadingTour:委托回调上触发一次。正如我现在的代码一样,它将为每个下载的文件创建一个观察者,并在每次下载文件时为所有以前的文件触发事件。每次下载文件时都会调用此方法 使用RAC处理每次完成的观察者的正确方法是,还是使用RAC的不同部分而不是subscribeNext来处理此模式 // In APIClient.swift

我不熟悉ReactiveCocoa,并尝试使用它构建API客户机。我希望RAC观察员在每个
URLSession:downloadTask:didFinishdownloadingTour:
委托回调上触发一次。正如我现在的代码一样,它将为每个下载的文件创建一个观察者,并在每次下载文件时为所有以前的文件触发事件。每次下载文件时都会调用此方法

使用RAC处理每次完成的观察者的正确方法是,还是使用RAC的不同部分而不是subscribeNext来处理此模式

// In APIClient.swift

func urlSessionDownloadTask(method: httpMethod, url: String, acceptHeader: String, progress: NSProgress?, success: (url: NSURL) -> Void , failure: (error: APIError) -> ()) -> NSURLSessionTask? {
    let url = NSURL(string: url)
    var urlRequest = NSMutableURLRequest(URL: url!, cachePolicy: .ReturnCacheDataElseLoad, timeoutInterval: 50)
    urlRequest.HTTPMethod = method.rawValue
    urlRequest.allHTTPHeaderFields![Constants.HTTPHeaderKeys.accept] = acceptHeader

    RACSignalSubscriptionNext(selector: Selector("URLSession:downloadTask:didFinishDownloadingToURL:"), fromProtocol: NSURLSessionDownloadDelegate.self) { (racTuple) -> Void in
        let urlSession = racTuple.first as NSURLSession
        let downloadTask = racTuple.second as NSURLSessionDownloadTask
        let location = racTuple.third as NSURL
        println(location)
        println("Did finish downloading file, should only be called once per file")
        success(url: location)
    }
    RACSignalSubscriptionNext(selector:Selector("URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:") , fromProtocol: NSURLSessionDownloadDelegate.self) { (racTuple) -> Void in
        dispatch_async(dispatch_get_main_queue(), {
             let totalBytesWritten = racTuple.fourth as NSNumber
             if let actualProgress = progress {
                 actualProgress.completedUnitCount = totalBytesWritten.longLongValue
             }
        })

    }
    let task = session.downloadTaskWithRequest(urlRequest, completionHandler: nil)
    task.resume()
}

//  just a convenice method to setup a next subscription
func RACSignalSubscriptionNext(#selector:Selector, fromProtocol: Protocol, subscribeNext: (racTuple: RACTuple) -> Void) {
        rac_signalForSelector(selector, fromProtocol:fromProtocol).subscribeNext { (anyObject) -> Void in
            if let racTuple = anyObject as? RACTuple {
                subscribeNext(racTuple: racTuple)
            } else {
                println("something wrong happened")
            }
        }
    }
我不使用完成处理程序downloadTaskWithRequest的原因是我想订阅多个
URLsessionDownloadDelegate
事件