Nested nsurl dataTaskWithRequest but after the completion handler called

98 Views Asked by At

I have this part of code and it was work with "NSURLConnection sendSynchronousRequest" and they was having a dispatch and have the both API call in it with no issues, however since i am moving to "NSURLSession" and i would to call the next API after the first one response inside the completionHandler, do i need tho add any type of dispatches for the second API call?

//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch was here

    __block NSString* url = @"www.google.com";
    [[LoaderService get] getRequestFrom:url completionHandler:^(TcHttpJSONResponse *response) { // first Call
        if (!response.success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                failBlock();
            });
            return;
        }
        
        url = [NSString stringWithFormat:@"www.google.com/drive"];
        [[LoaderService get] getRequestFrom:url completionHandler:^(TcHttpJSONResponse *response) { // second Call
            if (!response.success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failBlock();
                });
                return;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                successBlock(transactionsArray);
            });
        }];
    }];
//    }]; //end of dispatch

in LoaderService

- (void)getRequestFrom:(NSString *)url completionHandler:(void (^)(TcHttpJSONResponse *response))completionHandler {
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithRequest:[TcHttpHelper getRequestFromUrlWithAuthorizationToken:url token:[self getAuthToken]]
            completionHandler:^(NSData *data, NSURLResponse *response,  NSError *error) {
        completionHandler([[TcHttpJSONResponse alloc] initWithResponse:data urlResponse:(NSHTTPURLResponse*)response error:error]);
    }] resume];
}
0

There are 0 best solutions below