从NSURLResponse完成块中获取数据 [英] Getting data out of the NSURLResponse completion block
问题描述
看起来我还没有完全理解块的概念......
It looks like I didn't get the concept of blocks completely yet...
在我的代码中我必须从<$中获取JSON数据c $ c> asychronous block 从'外部
'方法返回。我用谷歌搜索,发现如果用__block 定义一个变量,那么该变量的变量扩展到<$的v̶i̶s̶i̶b̶i̶l̶i̶t̶y̶
。 _mutability_
c $ c>阻止
In my code I have to get out the JSON data from the asychronous block
to be returned to from the 'outer
' method. I googled and found that if defining a variable with __block
, the v̶i̶s̶i̶b̶i̶l̶i̶t̶y̶ _mutability_ of
that variable is extended to the block
.
但由于某些原因,返回的json对象为nil。我想知道为什么?
But for some reason returned json object is nil.I wonder why?
- (NSMutableDictionary *)executeRequestUrlString:(NSString *)urlString
{
__block NSMutableDictionary *json = nil;
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:YES];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSString *cookieString = [self.userDefaults objectForKey:SAVED_COOKIE];
[request addValue:cookieString forHTTPHeaderField:@"Cookie"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"dataAsString %@", [NSString stringWithUTF8String:[data bytes]]);
NSError *error1;
NSMutableDictionary * innerJson = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error1];
json = innerJson;
}];
return json;
}
推荐答案
首先,回答你的问题:
但由于某种原因,返回的json对象是
nil
。我想知道为什么?
您返回的变量在您返回时尚未设置。在 sendAsynchronousRequest:queue:completionHandler:
方法返回后,您无法立即收集结果:调用必须在回调块并设置 json
变量。
The variable that you are returning has not been set at the time when you return it. You cannot harvest the results immediately after the sendAsynchronousRequest:queue:completionHandler:
method has returned: the call has to finish the roundtrip before calling back your block and setting json
variable.
现在快速说明如何处理它:您的方法是尝试将异步调用转换为同步调用。如果可以,尽量保持异步。不要期望返回 NSMutableDictionary *
的方法,而是创建一个方法,该方法接受自己的块,并在时将字典传递给该块。 sendAsynchronousRequest:
方法完成:
Now a quick note on what to do about it: your method is attempting to convert an asynchronous call into a synchronous one. Try to keep it asynchronous if you can. Rather than expecting a method that returns a NSMutableDictionary*
, make a method that takes a block of its own, and pass the dictionary to that block when the sendAsynchronousRequest:
method completes:
- (void)executeRequestUrlString:(NSString *)urlString withBlock:(void (^)(NSDictionary *jsonData))block {
// Prepare for the call
...
// Make the call
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"dataAsString %@", [NSString stringWithUTF8String:[data bytes]]);
NSError *error1;
NSMutableDictionary * innerJson = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions error:&error1
];
block(innerJson); // Call back the block passed into your method
}];
}
这篇关于从NSURLResponse完成块中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!