user2640466
user2640466

Reputation: 31

iOS how get variable value from block?

Can i get variable value from block?

    - (Params *) getParams {
    __block Params* params = nil;
        //make post, get requests
    [JSONHTTPClient getJSONFromURLWithString:@"http://www.blankspot.ru/api/getinterval"
                                       params:nil
                                      completion:^(id json, JSONModelError *err) {
                                       NSLog(@"json = %@", json);
                                       NSLog(@"Error = %@", err );
                                      NSDictionary* json1 = json;
                                       NSLog(@" %@ ob ", [json1 objectForKey:@"success"]);
                                       params = [[Params alloc] initWithDictionary:json1 error:&err];

                                    NSLog(@"params123 = %@", params); // is not null
                                        }];

    NSLog(@"params123 = %@", params); //this is null
    return params;
   }

In first variant NSLog view non null value, but second variant after block will be nil.

Upvotes: 2

Views: 328

Answers (4)

BHASKAR
BHASKAR

Reputation: 1201

Used this:

-(void)getParams:(void(^)(Params *))finishBlock{
    [JSONHTTPClient getJSONFromURLWithString:@"http://www.blankspot.ru/api/getinterval"
                                      params:nil
                                  completion:^(id json, JSONModelError *err) {
                                      NSLog(@"json = %@", json);
                                      NSLog(@"Error = %@", err );
                                      NSDictionary* json1 = json;
                                      NSLog(@" %@ ob ", [json1 objectForKey:@"success"]);
                                      params = [[Params alloc] initWithDictionary:json1 error:&err];
                                      finishBlock(params);
                                      NSLog(@"params123 = %@", params); // is not null
                                  }];
}

Upvotes: 0

hybridcattt
hybridcattt

Reputation: 3041

Your problem is that your completion block is executed asynchronously after some other action (e.g. network request) will be complete (argument name "completion" should have given you a clue). It means that lines

NSLog(@"params123 = %@", params); //this is null
return params;

will be executed way before your block. And at that point params will still be nil.

If you need to process received data, you should do it inside your completion block. On the other hand, if you need to pass received parameters further, you'll have to create your own completion block parameter, because you get your result (Param *) after other operations are complete and not instantly.

Spoek suggested a good example, but don't forget to check success parameter for not being nil, otherwise when you'll try to call it, your app will crash.

Upvotes: 1

ullstrm
ullstrm

Reputation: 10160

What you should do is to not use a return-statement, but making your own block where the value is returned. Like this:

in .h:

-(void)getParamsSuccess:(void(^)(Params* params))success;

in .m:

-(void)getParamsSuccess:(void(^)(Params *))success; {
    //make post, get requests
[JSONHTTPClient getJSONFromURLWithString:@"http://www.blankspot.ru/api/getinterval"
                                   params:nil
                                  completion:^(id json, JSONModelError *err) {
                                   NSLog(@"json = %@", json);
                                   NSLog(@"Error = %@", err );
                                  NSDictionary* json1 = json;
                                   NSLog(@" %@ ob ", [json1 objectForKey:@"success"]);
                                   Params* params = [[Params alloc] initWithDictionary:json1 error:&err];

                                NSLog(@"params123 = %@", params); // is not null


                                   success(params); // <-- This will call the handler-block with your params-object as the parameter


                                    }];
}

Upvotes: 1

giorashc
giorashc

Reputation: 13713

This is because the getJSONFromURLWithString is asynchronous (It uses dispatch_async to make the request call) which means it will be called on another thread while the current thread will keep running.

Your NSLog shows nil since the block is probably executed some time after the NSLog line is reached due to the asynchronous nature of the call (as URL requests take some time to complete and are not immediate)

Upvotes: 2

Related Questions