iosobjective-cobjective-c-blocksnsurlsessionnshttpurlresponse

How to get result in blocks immediately?


I'm using blocks to get header fields from response in one class and I have to get that in another class.

I implemented code like this

In first class:

- (void)viewDidLoad {
    [super viewDidLoad];
    UserAuthentication *auth = [[UserAuthentication alloc]init];
    NSDictionary *dict = [auth getUserConfiguration];
    NSLog(@"%@",dict);
}

In userAuthentication class:

-(NSDictionary *)getUserConfiguration;
{
    __block NSDictionary *resultDictionary;
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                if ([response respondsToSelector:@selector(allHeaderFields)]) {
                     resultDictionary = [httpResponse allHeaderFields];
                    NSLog(@"%@",resultDictionary);
                }
            }] resume];
    NSLog(@"%@",resultDictionary);
    return resultDictionary;
}

Here my problem is in first class I'm getting dict as null.

Even in userAuthentication class also I'm getting null.

But after some time call back method is calling and then I can see the response correctly in completionHandler.

So how I can get response in firstClass?


Solution

  • You are misunderstanding the basic principle of async operation that runs in background thread and when the operation is completed it gives you data in completion block.

    To get response in viewDidLoad Method of second class you need to use blocks. like below

    -(void)getUserConfigurationOnCompletion:(void (^)(NSDictionary *))completion
    {
        __block NSDictionary *resultDictionary;
        NSURLSession *session = [NSURLSession sharedSession];
        [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"]
                completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    NSError *error) {
                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                    if ([response respondsToSelector:@selector(allHeaderFields)]) {
                         resultDictionary = [httpResponse allHeaderFields];
                       // Call completion with parameter
                        completion(resultDictionary);
                    }
                }] resume];
    }
    

    and use it like this in viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];
        UserAuthentication *auth = [[UserAuthentication alloc]init];
        [auth getUserConfigurationOnCompletion:^(NSDictionary *dict){
        // do necessary work with response dictionary here
        NSLog(@"%@",dict);
    
       }];
    }