取消的请求会花费大量时间来处理队列中的最后一个新请求

时间:2015-07-08 21:55:48

标签: ios swift afnetworking

我已经尝试了很多方法,仍然无法弄清楚如何解决这个问题。

我使用不同的参数创建url请求并显示微调器(异步请求)。但是我经常需要取消之前的请求并创建一个新请求。有时大约有20个已取消的请求,而且唯一一个是活动的,这是最后一个。

我用AFNetworking实施的最后一个例子:

if ConnectionManager.sharedInstance.businessOperation != nil {
    ConnectionManager.sharedInstance.businessOperation!.cancel()
}

let manager = ConnectionManager.sharedInstance.businessManager
manager.responseSerializer = AFJSONResponseSerializer()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: .allZeros)

var path = ConnectionManager.sharedInstance.path + ObjectPathComponent.Businesses.rawValue
path = path.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

NSLog("start loading")

ConnectionManager.sharedInstance.businessOperation = manager.GET(path,
    parameters: params,
    success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
        NSLog("loaded")

        ConnectionManager.sharedInstance.businessOperation = nil

        completionHandler(responseObject)
    },
    failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        if error.code != NSURLErrorCancelled {
            NSLog("business request error")
            completionHandler(error)
        }
        else {
            NSLog("cancelled")
        }
})

该日志:

2015-07-09 00:48:49.639 [97349:3984410] start loading
2015-07-09 00:48:50.060 [97349:3984504] loaded
2015-07-09 00:48:52.401 [97349:3984410] start loading
2015-07-09 00:48:52.820 [97349:3984505] loaded
2015-07-09 00:49:58.622 [97349:3984410] start loading
2015-07-09 00:50:03.364 [97349:3985559] loaded
2015-07-09 00:50:05.025 [97349:3984410] start loading
2015-07-09 00:50:05.534 [97349:3984410] start loading
2015-07-09 00:50:05.535 [97349:3985555] cancelled
2015-07-09 00:50:06.068 [97349:3984410] start loading
2015-07-09 00:50:06.069 [97349:3985555] cancelled
2015-07-09 00:50:06.546 [97349:3984410] start loading
2015-07-09 00:50:06.546 [97349:3985822] cancelled
2015-07-09 00:50:07.070 [97349:3984410] start loading
2015-07-09 00:50:07.070 [97349:3985558] cancelled
2015-07-09 00:50:09.366 [97349:3984410] start loading
2015-07-09 00:50:09.366 [97349:3985555] cancelled
2015-07-09 00:50:09.875 [97349:3984410] start loading
2015-07-09 00:50:09.876 [97349:3985559] cancelled
2015-07-09 00:50:10.407 [97349:3984410] start loading
2015-07-09 00:50:10.408 [97349:3985822] cancelled
2015-07-09 00:50:10.874 [97349:3984410] start loading
2015-07-09 00:50:10.875 [97349:3985822] cancelled
2015-07-09 00:50:11.344 [97349:3984410] start loading
2015-07-09 00:50:11.344 [97349:3985822] cancelled
2015-07-09 00:50:11.793 [97349:3984410] start loading
2015-07-09 00:50:11.793 [97349:3985559] cancelled
2015-07-09 00:50:12.297 [97349:3984410] start loading
2015-07-09 00:50:12.297 [97349:3985822] cancelled
2015-07-09 00:50:12.800 [97349:3984410] start loading
2015-07-09 00:50:12.800 [97349:3985822] cancelled
2015-07-09 00:50:13.315 [97349:3984410] start loading
2015-07-09 00:50:13.315 [97349:3984505] cancelled
2015-07-09 00:50:14.047 [97349:3984410] start loading
2015-07-09 00:50:14.048 [97349:3984505] cancelled
2015-07-09 00:50:14.603 [97349:3984410] start loading
2015-07-09 00:50:14.604 [97349:3984505] cancelled
2015-07-09 00:50:15.067 [97349:3984410] start loading
2015-07-09 00:50:15.067 [97349:3984505] cancelled
2015-07-09 00:50:15.601 [97349:3984410] start loading
2015-07-09 00:50:15.601 [97349:3985559] cancelled
2015-07-09 00:50:50.896 [97349:3986273] loaded

如果先看看,开始加载'之间的最长延迟时间为5秒。并且'已加载'。但是当许多请求被取消时,它可能超过30秒,这取决于请求的数量。

有没有办法改变上次请求的优先级?我需要尽可能快地完成请求,但我认为这个队列不允许我这样做。

很高兴听到任何想法。

0 个答案:

没有答案