Reputation: 31
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
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
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
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
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