如何同步两个NSURLSessionDataTasks?

时间:2014-12-12 13:42:16

标签: ios objective-c xcode concurrency

我不知道如何同步NSURLSessionDataTask。

如何在syncInfo1Info2中同步这两个会话:

- (void) syncInfo1Info2
{
  //How do I sync???
}

- (void) getSomeInfo1
{
NSString *stringToUrl = [[NSString alloc]initWithFormat:@"url"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringToUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLSessionDataTask *task =[session dataTaskWithRequest: request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
    //Some code...

        }];
[task resume];
}

- (void) getSomeInfo2
{
NSString *stringToUrl = [[NSString alloc]initWithFormat:@"url"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringToUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLSessionDataTask *task =[session dataTaskWithRequest: request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
   //Some code...

}];
[task resume];
}

3 个答案:

答案 0 :(得分:2)

像这样:

dispatch_group_t group = dispatch_group_create();

- (void) syncInfo1Info2
{
  [self getSomeInfo1];
  [self getSomeInfo2];
  dispatch_group_notify(group, dispatch_get_main_queue(), ^{
     //both tasks have been processed
  });
}

- (void) getSomeInfo1
{
    dispatch_group_enter(group);
    NSString *stringToUrl = [[NSString alloc]initWithFormat:@"url"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableURLRequest *requestRu = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringToUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    NSURLSessionDataTask *task =[session dataTaskWithRequest: requestRu completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
        NSError *err;
        dispatch_group_leave(group);
        }];
    [task resume];
}

- (void) getSomeInfo2
{
    dispatch_group_enter(group);
    NSString *stringToUrl = [[NSString alloc]initWithFormat:@"url"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableURLRequest *requestRu = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[stringToUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    NSURLSessionDataTask *task =[session dataTaskWithRequest: requestRu completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
        NSError *err;
        dispatch_group_leave(group);
    }];
    [task resume];
}

答案 1 :(得分:0)

您可以使用NSOperationQueue在操作之间添加依赖关系的简单方法

例如

NSOperationQueue  *operationQueue = [[NSOperationQueue alloc] init];

NSBlockOperation *downloadOperation1 = [[NSBlockOperation alloc] init];
 __weak NSBlockOperation *weakOperation1 = downloadOperation1;

[weakOperation1 addExecutionBlock:^{
    [self getSomeInfo1];
}];


NSBlockOperation *downloadOperation2 = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakOperation2 = downloadOperation2;

[weakOperation2 addExecutionBlock:^{
    [self getSomeInfo2];
}];

[downloadOperation2  addDependency:downloadOperation1];
[operationQueue addOperation:downloadOperation1];
[operationQueue addOperation:downloadOperation2];

或使用此link

中给出的方法

在类NSURLSession属性设置为1的类中创建HTTPMaximumConnectionsPerHost单例:

+ (NSURLSession *)session
{
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

        [configuration setHTTPMaximumConnectionsPerHost:1];

        session = [NSURLSession sessionWithConfiguration:configuration];

    });
    return session;
}

然后使用

NSURLSessionDataTask *dataTask = 
[[[self class] session] dataTaskWithURL:url 
                          completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}
[dataTask resume]

答案 2 :(得分:0)

我们可以将api gunc放在一个类中并在它上面创建一个共享实例,在视图控制器类中我们可以使用像

这样的类实例进行调用

ApiClient.shared.api1() ApiClient.shared.api2()

由于我们有一个共享实例,api调用将被同步