Reputation: 1
I don't know how to synchronize NSURLSessionDataTask.
How do I synchronize these two sessions in 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];
}
Upvotes: 0
Views: 1201
Reputation: 214
We can put the api gunc in a class and creaye a shared instance on it and in view controller class we can just make call with the class instance like
ApiClient.shared.api1() ApiClient.shared.api2()
Since we have a shared instance api calls will be synchronized
Upvotes: 0
Reputation: 12023
Simple way you can use NSOperationQueue
to add dependencies between operations
eg.
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];
or use the method given in this link
create NSURLSession
singleton in class that has its HTTPMaximumConnectionsPerHost
property set to 1:
+ (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;
}
and Then use
NSURLSessionDataTask *dataTask =
[[[self class] session] dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}
[dataTask resume]
Upvotes: 0
Reputation: 1547
Like this:
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];
}
Upvotes: 3