iOS使用NSOperationQueue和AFNetworking实现多下载功能

iOS使用NSOperationQueue和AFNetworking实现多下载功能,ios,afnetworking-2,nsoperation,nsoperationqueue,Ios,Afnetworking 2,Nsoperation,Nsoperationqueue,我正在使用NSOperationQueue和AFNetworking为我的项目实现多重下载功能。我也使用核心数据来保存下载的信息,但我同意这一部分。我的问题是在实际创建下载任务时。我是这样做的: // create a NSOperationQueue NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.name = @"DownloadQueue"; queue.maxConcurrentOperationCount

我正在使用NSOperationQueue和AFNetworking为我的项目实现多重下载功能。我也使用核心数据来保存下载的信息,但我同意这一部分。我的问题是在实际创建下载任务时。我是这样做的:

// create a NSOperationQueue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.name = @"DownloadQueue";
queue.maxConcurrentOperationCount = 1;

// create an operation (AFDownloadRequestOperation is a subclass of AFHTTPRequestOperation)
AFDownloadRequestOperation *operation = // some initializations

// add the operation to the queue
[queue addOperation:operation];

// later some more operations are created and added to the queue
然后假设我在队列中添加了3个操作。由于我将maxConcurrentOperationCount设置为1,因此队列中只有1个操作正在运行,其他2个正在等待:

operation 1: running
operation 2: waiting
operation 3: waiting
然后,我使用PauseOperation(AFURLConnectionOperation的一种方法)暂停正在运行的操作,我希望看到两个等待操作中的一个开始运行,但没有发生,两个等待操作仍在等待:

operation 1: paused
operation 2: waiting
operation 3: waiting
我仍然必须恢复操作1并等待它完成,然后操作2或3将开始运行

我的问题是,在我暂停一个正在运行的操作之后,如何让队列中的其他操作运行,就像我上面解释的那样

另外,如果您能解释一种更好的实现多重下载的方法,特别是与管理最大数量的下载任务、暂停/恢复下载任务相关的方法,我将不胜感激。

解决方案:

只需使用“取消”暂停操作,您就需要处理操作中的“取消”标志为“是”的情况,才能真正让操作退出。然后,队列行为中的所有操作如我所期望的那样取消一个操作,然后另一个等待的操作开始运行

当您需要恢复下载时,只需创建一个新操作,将内容从web获取到相同的本地文件路径。在AFDownloadRequestOperation的帮助下,恢复下载效果良好

现在我的多重下载系统运行良好,如果您需要有关实现的信息,请发表评论