gpt4 book ai didi

iphone - 如何使用 AFNetworking 中的 enqueueBatchOfHTTPRequestOperations 在操作失败时重新添加操作

转载 作者:行者123 更新时间:2023-11-29 13:21:49 24 4
gpt4 key购买 nike

我有一个方法可以使用 AFNetworking 调用 enqueueBatchOfHTTPRequestOperations 从 Web 服务器将文件列表下载到 iPad。每隔一段时间我就会遇到一个失败的操作(通常是在文件较大时)。过去,我单独下载每个文件,然后简单地再次调用该操作直到达到一定的重试次数。

我重构了代码以使用 enqueueBatchOfHTTPRequestOperations。这很好用,但我不知道如何获得有关“特定”操作失败的通知,而且我不知道如果它们失败了如何将它们重新添加到队列中。

这是我用来下载“n”个文件的代码:

    NSMutableArray *operationsArray = [[NSMutableArray alloc]init];
for (MINPageDefinition *currPage in [[MINPageStore sharedInstance] pageArray])
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *serverLibraryURL = [defaults objectForKey:kRootURL];
serverLibraryURL = [serverLibraryURL stringByAppendingPathComponent:kPageDefinitionsDirectory];
serverLibraryURL = [serverLibraryURL stringByAppendingPathComponent:currPage.pageImageName];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serverLibraryURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:currPage.pageImageURL append:NO];

[operationsArray addObject:operation];
}
if ([operationsArray count] > 0)
{
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
void (^progressBlock)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) = ^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%d proccesses completed out of %d", numberOfCompletedOperations, totalNumberOfOperations);
};
void (^completionBlock)(NSArray *operations) = ^(NSArray *operations) {
NSLog(@"All operations completed");
};
[client enqueueBatchOfHTTPRequestOperations:operationsArray
progressBlock:progressBlock
completionBlock:completionBlock];
}

之前,我有一个执行操作的方法。失败时,失败 block 将递归调用自身。如果操作失败,我如何修改此代码以最多重试“n”次?

最佳答案

检查下面的代码@justLearningAgain

[[WfServices sharedClient] GET:kGetAddress parameters:dicParam  success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"RESPONSE ::: %@",responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];


+ (WfServices *)sharedClient
{
static WfServices * _sharedClient = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
_sharedClient = [[WfServices alloc] initWithBaseURL:[NSURL URLWithString:kWFBaseURLString]];
});

return _sharedClient;
}

关于iphone - 如何使用 AFNetworking 中的 enqueueBatchOfHTTPRequestOperations 在操作失败时重新添加操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com