Ios Restkit RKObjectManager在下载大量文件时请求超时
首先:我对RestKit很陌生,所以这可能是一个很容易解决的问题 我正在尝试下载很多文件。目前,我使用Ios Restkit RKObjectManager在下载大量文件时请求超时,ios,objective-c,restkit,restkit-0.20,Ios,Objective C,Restkit,Restkit 0.20,首先:我对RestKit很陌生,所以这可能是一个很容易解决的问题 我正在尝试下载很多文件。目前,我使用RKObjectManager的getObjectsAtPath:parameters:success:failure:方法获取对象并将其映射到restkit 然而,它似乎过早地启动了一些下载,然后当它们在队列中时超时 我正在使用的代码: - (void)removeResponseAndRequestDescriptors { RKObjectManager *objectManage
RKObjectManager
的getObjectsAtPath:parameters:success:failure:
方法获取对象并将其映射到restkit
然而,它似乎过早地启动了一些下载,然后当它们在队列中时超时
我正在使用的代码:
- (void)removeResponseAndRequestDescriptors
{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager.requestDescriptors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[objectManager removeRequestDescriptor:obj];
}];
[objectManager.responseDescriptors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[objectManager removeResponseDescriptor:obj];
}];
}
它似乎一次启动所有下载,但实际下载并未启动。所以最后一次下载会超时
是否有更好的方法来解决此问题?将队列上的最大并发操作数设置为合理的值,如5
[objectManager.HTTPClient.operationQueue setMaxConcurrentOperationCount:5];
(在启动任何请求之前)所有下载都将同时运行,因为您正在进行一行中的所有
getPath:
调用(它们是异步调用)。由于每次下载都需要一段时间才能完成,因此会导致以后的调用超时
如果希望每次下载仅在前一次下载完成后进行,我将创建一个名为getNextAudioFile:
的方法和一个迭代器类属性。然后,在getPath:
的success
和failure
块中,增加迭代器并调用getNextAudioFile:
示例代码:
- (void)downloadAudioFiles
{
// No for loop
self.iterator = 0;
// your call to DownloadAudioFileForAudio: ... for the first audio goes here
}
- (void)downloadAudioFileForAudio:(IBAudio *)audio
inBook:(IBBook *)book
downloadStatus:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))downloadStatus
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
// your code ...
[objectManager.HTTPClient getPath:[IBProperties downloadAudioEndPointWithIsbn:book.isbn andAnchor:audio.anchor] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
// your code...
// increment and get the next file
self.iterator++;
[self getNextAudioFile];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
// increment and get the next file
self.iterator++;
[self getNextAudioFile];
}];
}
- (void)getNextAudioFile
{
if(self.iterator < [self.book.bookData.audios count]){
// make your downloadAudioFileForAudio: call for the next audio
}
}
-(无效)下载音频文件
{
//循环无
self.iterator=0;
//您对DownloadAudioFileForAudio的呼叫:…第一个音频在这里
}
-(无效)下载音频文件用于音频:(IBAudio*)音频
inBook:(iBook*)书
下载状态:(void(^)(nsinteger字节读取,long-long-totalBytesRead,long-long-totalBytesExpectedToRead))下载状态
成功:(无效(^)(无效))成功
失败:(无效(^)(n错误*错误))失败
{
//你的代码。。。
[objectManager.HTTPClient getPath:[IBProperties downloadAudioEndPointWithIsbn:book.isbn and anchor:audio.anchor]参数:无成功:^(AFHTTPRequestOperation*操作,id响应对象){
//你的代码。。。
//递增并获取下一个文件
self.iterator++;
[self-getNextAudioFile];
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
失败(错误);
//递增并获取下一个文件
self.iterator++;
[self-getNextAudioFile];
}];
}
-(void)getNextAudioFile
{
if(self.iterator<[self.book.bookData.audios count]){
//将下载的音频文件设置为音频:调用下一个音频
}
}
至少是这样!希望有帮助
[objectManager.HTTPClient.operationQueue setMaxConcurrentOperationCount:5];
- (void)downloadAudioFiles
{
// No for loop
self.iterator = 0;
// your call to DownloadAudioFileForAudio: ... for the first audio goes here
}
- (void)downloadAudioFileForAudio:(IBAudio *)audio
inBook:(IBBook *)book
downloadStatus:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))downloadStatus
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
// your code ...
[objectManager.HTTPClient getPath:[IBProperties downloadAudioEndPointWithIsbn:book.isbn andAnchor:audio.anchor] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
// your code...
// increment and get the next file
self.iterator++;
[self getNextAudioFile];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
// increment and get the next file
self.iterator++;
[self getNextAudioFile];
}];
}
- (void)getNextAudioFile
{
if(self.iterator < [self.book.bookData.audios count]){
// make your downloadAudioFileForAudio: call for the next audio
}
}