AFNetworking超过3g的批量POST请求失败

时间:2015-07-18 11:08:26

标签: objective-c afnetworking batch-processing 3g operations

我使用以下代码作为初始同步的一部分,并为应用程序提取刷新同步代码。当设备在3g下运行时,POST调用超时,我收到错误:

代码= -1005"网络连接丢失。"

在线查找相同的问题后,我添加了一个操作队列并使用了:

manager.operationQueue.maxConcurrentOperationCount = 2;

将最大并发操作设置为低于3阈值,这似乎是Apple的问题。然而,无论我将maxOperationCount设置为应用程序的数量,似乎同时运行4个操作并且3个工作并且第四次运行并且失败。这具有使同步永远持续的效果。

我看不到任何与maxConcurrentOperationCount相关的任务没有工作,所以它必须是我代码中的东西?

我看不出我做错了什么。

由于

AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] init];

NSMutableArray *operationArray = [[NSMutableArray alloc] init];

for (NSDictionary *entityDict in entityArray){

    NSDictionary *searchData = @{@"userUUID": [[NSUserDefaults standardUserDefaults] valueForKey:@"uuid"],
                                 @"action":@"SELECTrecordsForUser",
                                 @"tableName": [NSString stringWithFormat:@"%@%@", entityDict[@"prefix"], entityDict[@"entityName"]],
                                 @"fromDate":entityDict[@"fromDate"],
                                 @"entityName":entityDict[@"entityName"],
                                 @"record":@{@"uuid":entityDict[@"uuid"]}
                                 };


    //Create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@", global_baseURLStringJSON, global_RESTfulFileName] parameters:searchData error:nil];

    //Create operation
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

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

        NSLog (@"Success");

        NSArray * responseArray = responseObject;

        //Add record details from server into entity in local database
        [self populateLocalDatabase:entityDict[@"entityName"] withData:responseArray];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        //Download failed add to queue for redownload
        NSLog (@"Failed: %@", error);
        [self addToSyncQueue:entityDict forAction:@"SELECTrecordsForUser"];

    }];


    //Add operation to array
    [operationArray addObject:operation];

}

//Track status of operations
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:operationArray
                                                           progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

                                                               NSLog (@"Items in Queue %lu", (unsigned long)manager.operationQueue.operationCount);

                                                               NSLog(@"Finished:%f   Total:%lu", (float)numberOfFinishedOperations, (unsigned long)totalNumberOfOperations);

                                                           } completionBlock:^(NSArray *operations) {

                                                              NSLog (@"all done");

                                                               [[NSUserDefaults standardUserDefaults] setInteger: 1 forKey:@"syncComplete"];

                                                           }];

//Add to Operation manager
manager.operationQueue.maxConcurrentOperationCount = 2;
[manager.operationQueue addOperations:operations waitUntilFinished:NO];

日志中的示例错误

2015-07-18 11:58:15.899 crew[1649:444527] Items in Queue 25
2015-07-18 11:58:15.899 crew[1649:444527] Finished:1.000000   Total:25
2015-07-18 11:58:15.900 crew[1649:444527] Success
2015-07-18 11:58:15.967 crew[1649:444527] → Saving <NSManagedObjectContext (0x1701f3f00): MagicalRecord Default Context> on the main thread

2015-07-18 11:58:15.969 crew[1649:444527] Items in Queue 24
2015-07-18 11:58:15.970 crew[1649:444527] Finished:2.000000   Total:25
2015-07-18 11:58:15.971 crew[1649:444527] Updated Entity (null)
2015-07-18 11:58:15.972 crew[1649:444527] Success
2015-07-18 11:58:16.116 crew[1649:444527] → Saving <NSManagedObjectContext (0x1701f3f00): MagicalRecord Default Context> on the main thread

2015-07-18 11:58:20.886 crew[1649:444527] Items in Queue 23
2015-07-18 11:58:20.887 crew[1649:444527] Finished:3.000000   Total:25
2015-07-18 11:58:20.887 crew[1649:444527] Failed: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x1700f0a00 {NSUnderlyingError=0x174441f50 "The network connection was lost.", NSErrorFailingURLStringKey=http://www.xxxxxxxx.com/xxxxxx/xxxxxx.php, NSErrorFailingURLKey=http://www.xxxxxxxx.com/xxxxxx/xxxxxx.php,, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}

0 个答案:

没有答案