问题
I have problem with this block. I trying to get the data inside the block of NSURLSession
.
here's my code
-(NSDictionary *) RetrieveData{
NSURLSession * session = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString: self.getURL];
dataList =[[NSDictionary alloc] init];
NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}];
return self.dataList;
[dataTask resume];
}
Is it possible to get the data inside the blocks of NSURLSession
?
回答1:
-(void)getJsonResponse:(NSString *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure
{
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:urlStr];
// Asynchronously API is hit here
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",data);
if (error)
failure(error);
else {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",json);
success(json);
}
}];
[dataTask resume]; // Executed First
}
call this:
[self getJsonResponse:@"Enter your url here" success:^(NSDictionary *responseDict) {
NSLog(@"%@",responseDict);
} failure:^(NSError *error) {
// error handling here ...
}];
回答2:
You should use a completion block, e.g.:
- (void)retrieveData:(void (^)(NSDictionary * dictionary))completionHandler {
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString: self.getURL];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (completionHandler) {
completionHandler(dictionary);
}
}];
[dataTask resume];
}
Then the method that calls this would do:
[self retrieveData:^(NSDictionary *dictionary) {
// you can use the dictionary here
// if you want to update UI or model, dispatch this to the main queue:
dispatch_async(dispatch_get_main_queue(), ^{
// do your UI stuff here
});
}];
// but dont try to use the dictionary here, because you will likely
// hit this line before the above block fires off, and thus the
// dictionary hasn't been returned yet!
You're calling a asynchronous method that employs a completion block pattern, so you should also employ the completion block pattern in your own code.
回答3:
You'll have to get your head around this method. Best you rename your method from RetrieveData (you are violating Cocoa naming conventions here) to startRetrievingData. You can't write a method actually retrieving the data, because that can take minutes in the worst case and your users will hate you.
Call it startRetrievingData, make it return void, and pass in two blocks, when that will be called in the future when the data has been retrieved, and one that will be called in the future when an error has happened and you can't get the data.
You can't return the data. Don't ask "how do I return data", you just can't. You give the code a block that is called when data is available, and that block is responsible for doing with the data whatever you want to do with it.
回答4:
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"http://www.code-brew.com/projects/Instamigos/api/login.php?instagram_id=572275360&access_token=572275360.4c70214.57e0ecb1113948c2b962646416cc0a18&name=dpak_29&uuid=ios_1"];
// Asynchronously API is hit here
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",data);
// Executed when the response comes from server
// Handle Response here
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",json);
}];
[dataTask resume]; // Executed First
来源:https://stackoverflow.com/questions/26174692/how-to-get-data-from-blocks-using-nsurlsession