ios - 有没有一种巧妙的方法可以将完成 block 附加到 Swift 中的 NSURLSessionDataDelegate 回调?

标签 ios swift nsurlsession completion-block

好的,交易如下:

我正在 Swift 应用程序中进行 URL 调用,有点像这样:

/*!
    @brief Tests a given Root Server URL for validity

    @discussion What we do here, is append "/client_interface/serverInfo.xml"
                to the given URI, and test that for validity.

    @param inURIAsAString This contains a string, with the URI.

    @param inCompletionBlock This is the completion block supplied by the caller. It is to be called upon receipt of data.

    @returns an implicitly unwrapped optional String. This is the given URI, "cleaned up."
*/
class func testRootServerURI(inURIAsAString:String, inCompletionBlock:requestCompletionBlock!) -> String! {
    // First, trim off any trailing slashes.
    var ret:String! = inURIAsAString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "/"))
    // Next, make the string entirely lowercase.
    ret = ret.lowercaseString

    // Add the "http://" if necessary.
    if(!ret.beginsWith ("http")) {
        ret = "http://" + ret
    }

    // This is the URI we will actually test.
    let testURIString = ret + "/client_interface/serverInfo.xml"

    #if DEBUG
        print("Testing \(testURIString).")
    #endif

    let url:NSURL! = NSURL(string: testURIString)

    // We can't have the URL already in play. That would be bad.
    if(nil == self.urlExtraData[testURIString]) {
        // Assuming we have a completion block and a URI, we will actually try to get a version from the server (determine its validity).
        if((nil != inCompletionBlock) && (nil != ret)) {
            // Store the completion block for recall later.
            self.urlExtraData.updateValue(inCompletionBlock, forKey: testURIString)
            let dataTask:NSURLSessionTask = BMLTAdminAppDelegate.connectionSession.dataTaskWithURL(url)!

            dataTask.resume()
        }
    }
    else {
        ret = nil
    }

    return ret
}

实际上,完全一样,因为它是我正在使用的函数(静态类函数)。

有问题的一行是这一行:

self.urlExtraData.updateValue(inCompletionBlock, forKey: testURIString)

“urlExtraData”是我之前声明的字典:

/*!
    This is a dictionary of callbacks for open requests. It keys on the URL called.

    @discussion I hate pulling crap like this, as it's clumsy and thread-unfriendly. Unfortunately,
                there doesn't seem to be much choice, as there's no way to attach a refCon to a URL
                task.
*/
static var urlExtraData:Dictionary<String,requestCompletionBlock!>! = nil

并在此处分配:

// Set up an empty dictionary for the URL refCon data.
BMLTAdminAppDelegate.urlExtraData = Dictionary<String,requestCompletionBlock!>()

完成 block typedef 在这里:

/*!
    @brief This is the definition for the testRootServerURI completion block.

    @discussion The routine is called upon completion of a URL connection. When the
                connection ends (either successfully or not), this routine is called.
                If it is successful, then the inData parameter will be non-nil.
                If it failed, then the parameter will be nil.

    @param inData   the Data returned.
*/
typealias requestCompletionBlock = (inData: NSData!)->Void

session 设置在这里:

BMLTAdminAppDelegate.connectionSession = NSURLSession(configuration: config, delegate:self, delegateQueue: NSOperationQueue.mainQueue())

委托(delegate)响应处理程序位于此处:

/*!
    @brief Called when a task receives data.

    @param session The NSURLSession that controls this task.
    @param dataTask The task responsible for this callback.
    @param data The data returned.
*/
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
    let url                                 = dataTask.currentRequest?.URL
    // We don't do squat if there is no callback in our table.
    if(nil != BMLTAdminAppDelegate.urlExtraData.indexForKey((url?.absoluteString)!)) {
       // Fetch the stored comnpletion block from our dictionary.
        let callback:requestCompletionBlock!    = BMLTAdminAppDelegate.urlExtraData[(url?.absoluteString)!]

        // If one was provided (always check), then call it.
        if(nil != callback) {
            BMLTAdminAppDelegate.urlExtraData.removeValueForKey((url?.absoluteString)!) // Remove the callback from the dictionary.
            callback(inData: data)
        }
    }
}

我认为使用字典来保存第二个回调是一种令人讨厌的恶作剧。但是,我找不到将 refCon 附加到任务的方法。我宁愿将辅助完成 block 直接附加到任务,而不是使用单独的字典。

我很乐意被告知我的方法很糟糕,只要给我一些更好的东西。

有人接受吗?

谢谢!

最佳答案

我过去所做的是创建某种交易对象。我设置了一个下载管理器(作为单例)来创建一个事务对象数组。我使事务对象成为 URL session 的委托(delegate)(实际上这早于 NSURLSession - 我用 NSURLConnection 做到了,但想法是一样的。)

交易对象还有一个完成 block 参数。 然后当下载完成时,事务对象调用它的完成 block ,然后通知下载管理器单例它已经完成并准备好被清理。

关于ios - 有没有一种巧妙的方法可以将完成 block 附加到 Swift 中的 NSURLSessionDataDelegate 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834596/

相关文章:

ios - 预加载 Collection View (以便滚动平滑)

ios - 在 tableviewheader 中使用 UITextField 进行 self.view.endEditing 时崩溃

ios - iOS NSURLSession 是否支持 HTTP 100-continue?

ios - 在 View Controller 之间共享 NSURLsession 委托(delegate)实现

background - CFNetwork SSLHandshake failed (-9806) error using NSURLSession when app in background for an uploadTask

iphone - 解码字符串时获取垃圾和特殊字符

ios - 应用商店 : Make in app purchase item free for 1 day

iOS7 自定义 UTI

ios - 在开发人员和非开发人员之间共享 ios 开发

swift - 每当我的 TextView 更新时,我的 GameScene 都会重新加载