上传流程会阻止其他请求AFNetworking 3.0

时间:2016-10-01 05:06:52

标签: ios objective-c afnetworking-3

我正在尝试使用AFNetworking上传数据及其成功。没有问题,但问题是当我在后台执行它而不是其他请求块时,并且在上载进度完成之前不会给出任何响应。

我所做的是我制作了一个单身并声明AFURLSessionManager,因为我想要所有当前正在运行的任务。

+ (id)sharedInstance
{
    static SharedManager *sharedMyManager = nil;
    @synchronized(self) {
    if (sharedMyManager == nil)
        sharedMyManager = [[self alloc] init];
    }

    return sharedMyManager;
} 

让我告诉你一个简单的例子..对于一个控制器,我提出了上传过程的请求,如下所示

ViewController1.m

-(void)postImage{
    @try {
        NSMutableDictionary *otherParameters = [[NSMutableDictionary alloc]initWithDictionary:_currentPlaceParameters];
        [otherParameters setObject:[UtitlityManager getUserID] forKey:@"user_id"];

        AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
        NSMutableURLRequest *request =
        [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_PHOTO_VIDEO parameters:otherParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            NSData *thumbImageData = UIImageJPEGRepresentation(dataValue, 0.5);
            [formData appendPartWithFileData:thumbImageData name:@"thumbnail" fileName:@"image.jpg" mimeType:@"image/jpeg"];
        } error:nil];

        //show progress bar on window

        AFURLSessionManager *manager = [[SharedManager sharedInstance]manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSURLSessionUploadTask *uploadTask;
        uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
            // This is not called back on the main queue.
            // You are responsible for dispatching to the main queue for UI updates
            float uploadActualPercentage = uploadProgress.fractionCompleted * 100;
            NSLog(@"Multipartdata upload in progress: %@",[NSString stringWithFormat:@"%f %%",uploadActualPercentage]);
        }
                                          completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                                              if (error) {
                                              } else {
                                                  NSDictionary *result=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
                                                  if([[result objectForKey:@"success"] integerValue]==1){

                                                  }else{
                                                      [Common showMessageWithMessage:@"Failed, Try again"];
                                                  }

                                              }
                                          }];        
        [uploadTask resume];
    } @catch (NSException *exception) {
        NSLog(@"EXC: %@",[exception description]);
    }
}

而且我试图移动另一个视图控制器并发送获取其他数据的请求......

ViewController2.m

-(void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];

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

    }];
 }

但是直到进展结束才会给出回应......为什么呢?

即使我尝试在dispatch_sync中设置此上传​​过程但它不起作用......对此有任何建议???

0 个答案:

没有答案