How manipulate Block Objective C in AFNetworking?

半城伤御伤魂 提交于 2019-12-22 18:36:56

问题


What I do is this: My class1 (called a method, and expects llamda) -> class2 (also called a method and waiting for a call) -> class3 (recently this class queries the JBoss server AFNetworking) . And use the method AFNetworking:

- (AFHTTPRequestOperation *) POST: (NSString *) URLString
                       parameters: (NSDictionary *) parameters
        constructingBodyWithBlock: (void (^) (id <AFMultipartFormData> FormData)) block
                          success: (void (^) (* AFHTTPRequestOperation operation, responseObject id)) success
                          failure: (void (^) (* AFHTTPRequestOperation operation, NSError * error)) failure; 

And if or when the response returns me a block. And that answer myself need to return to the class2 and class1 to class2. And class1 performs some action according to the response.

How I can fix this?

Classes1 Method:

- (IBAction)pressedButton_createdUser:(id)sender
{
    NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

    NSDictionary *resultDict = [_mainViewController.restServices newUserNewDevice:xxx
                                                                         email:xxx
                                                                        password:xxx
                                                                            name:xxx
                                                                         surname:xxx
                                                                     phonenumber:xxx
                                                             currentLanguageCode:xxx
                                                                   isNewBusiness:xxx
                                                                      nameOrCode:xxx];

    NSLog(@"%@",[resultDict objectForKey:@"response"]);
}

Classes2 Methods Call:

- (NSDictionary *)newUserNewDevice:(UIImage *)photo email:(NSString *)email password:(NSString *)password name:(NSString *)name surname:(NSString *)surname phonenumber:(NSString *)phonenumber currentLanguageCode:(NSString *)currentLanguageCode isNewBusiness:(BOOL)isNewBusiness nameOrCode:(NSString *)nameOrCode
{   
    [_webservicesTask addParams:TAG_PHOTO Object:dataPhoto];
    .
    .
    [_webservicesTask addParams:TAG_NAME_OR_CODE Object:nameOrCode];

    [_webservicesTask executeMultipart:userRestPath]];
    return [json objectWithString:_strResult];
}

And finally this class use AFNetworking and return result, but result in block:

classes3
- (void)executeMultipart:(NSString *)url
{
    [_operationManager POST:url parameters:_params
  constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
      [formData appendPartWithFormData:(NSData *)[_params objectForKey:TAG_PHOTO] name:TAG_PHOTO];
  } success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"%@",operation.responseString);
      NSString *strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      _objREST.strResult = strJSON;
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"%@",operation.responseString);
      NSString *strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      _objREST.strResult = strJSON;
  }];
}

Already tried everything I think. Can someone please explain me how I can solve this problem. Thanks


回答1:


Try this:

manager.operationQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);


来源:https://stackoverflow.com/questions/21640102/how-manipulate-block-objective-c-in-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!