1

我有一种方法可以使用 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];
}

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

4

3 回答 3

1

检查下面的代码@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;
}
于 2014-07-15T13:17:14.133 回答
0

我认为AFNetworking 自动重试可能会有所帮助。

或者您可以在 AFHTTPClient 中覆盖以下方法。

考虑 resumeTasksArray = 您需要重新添加到队列中的操作数组。

NSMutableArray *resumeTasksArray; before @implementation AFHTTPClient 

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
                                                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                                failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {

void(^authFailBlock)(AFHTTPRequestOperation *opr, NSError *err) = ^(AFHTTPRequestOperation *opr, NSError *err){
    [resumeTasksArray addObject:request];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        //now, queue up and execute the original task
        for (NSURLRequest *previousRequest in resumeTasksArray) {
            NSLog(@"^^^^^^^^^^^^^^^^^^^^^^^^^^^^Resume Task URL - %@", previousRequest.URL);
            NSMutableURLRequest *newRequest = [previousRequest mutableCopy];
            [newRequest setValue:[NSString stringWithFormat:@"Bearer %@", AppSingleton.access_token] forHTTPHeaderField:@"Authorization"];
            AFHTTPRequestOperation *opera10n = [[AFHTTPRequestOperation alloc]initWithRequest:newRequest];
            opera10n.responseSerializer = self.responseSerializer;
            [opera10n setCompletionBlockWithSuccess:success failure:failure];
            if (![[self.operationQueue operations] containsObject:opera10n]) {
                [[self operationQueue] addOperation:opera10n];
                [opera10n start];
            }
        }
        [resumeTasksArray removeAllObjects];
    });
};
AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:request success:success failure:authFailBlock];
return operation;
}
于 2014-07-16T10:39:49.570 回答
0

您不能像这样将单个失败的操作再次排入该操作的失败块中吗?

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     [[APIClient sharedClient] enqueueHTTPRequestOperation:operation];
}];

[APIClient sharedClient]对您的子类的引用在哪里AFHTTPClient(单例与否)。

于 2013-01-31T17:02:13.143 回答