如何使用NSURLSession从块获取数据?

use*_*576 8 objective-c ios objective-c-blocks nsurlsession

我对这个块有问题.我试图获取块内的数据NSURLSession.

这是我的代码

-(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];

}
Run Code Online (Sandbox Code Playgroud)

是否可以获取块内的数据NSURLSession

neo*_* D1 27

-(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
}
Run Code Online (Sandbox Code Playgroud)

叫这个:

[self getJsonResponse:@"Enter your url here" success:^(NSDictionary *responseDict) {   
        NSLog(@"%@",responseDict);
    } failure:^(NSError *error) {
        // error handling here ... 
}];
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 8

您应该使用完成块,例如:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

然后调用它的方法会:

[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!
Run Code Online (Sandbox Code Playgroud)

您正在调用采用完成块模式的异步方法,因此您还应该在自己的代码中使用完成块模式.