Segev
Segev

Reputation: 19303

Can't get a JSON back from POST - Incompatible block pointer types

I wrote a POST method that needs to return a jSON to the viewcontroller that called her. If i add in the success block return _jsonDictionary; I will get this error:

Incompatible block pointer types sending 'id (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, __strong id)' to parameter of type 'void (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, __strong id)'

I guessing that because it's asynchronous, adding a return will force it to be synchronous but, I want all my POST methods for my app to be in one class so getting the data out of the JSON to variables that are declared across my app using something like valueForKey makes things a bit complicated for me. Is that bad design?

    -(NSDictionary *)getData
    {   
        _jsonDictionary = [[NSDictionary alloc]init];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/getSomething",MainURL ]];

        [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:nil parameters:nil];    

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
        {
            _jsonDictionary = JSON;

            NSLog(@"jsonDictionary: %@",_jsonDictionary);
        }
        failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
        {
            NSLog(@"request: %@",request);
            NSLog(@"Failed: %@",[error localizedDescription]);
        }];

        [httpClient enqueueHTTPRequestOperation:operation];
}

Another question, why do I get this warning at the end of the above code: Control reaches end of non-void function even if i change the name of the method in .m and in .h to -(void )getData ??

Upvotes: 3

Views: 514

Answers (3)

Daij-Djan
Daij-Djan

Reputation: 50089

you're totally mixing asyn and sync ... there IS valid no _jsonDictionary that getData could return. the _jsonDictionary is only filled asynchronously when the completion blocks are called.

you need to proceed from there.... call another method for example


as for the error/warning you see ... getData is supposed to return something (NSDictionary*) or a void* (almost eqaul to an id)

to not return stuff, it is void only.

Upvotes: 0

teriiehina
teriiehina

Reputation: 4761

If your main concern is to "get back" your data, you have three ways (maybe more, but I can only things of those three):

  1. in your getData method, you can post a NSNotification that your viewController subscribed to before calling getData
  2. if you are using (or plan to use) a dataManager as a singleton, your viewController can KVO on the @property of the dataManager
  3. my favorite: in your calling viewController, you construct and pass a block to your getData method that will be called (with or w/o the result). This is exactly what you are doing when building the AFJSONRequestOperation in your example.

Upvotes: 1

yonosoytu
yonosoytu

Reputation: 3319

The error you are getting is correct. The block is not supposed to return anything, and your return _jsonDictionary; is trying just that.

What you need to do is update _jsonDictionary inside the success block (like you already do), and then invoke another function to fire up events that refresh you UI (something like calling [self.tableView refreshData]).

Upvotes: 0

Related Questions