Managing two NSURLConnection

前端 未结 3 473
终归单人心
终归单人心 2021-01-23 11:04

I want to do two async request from two different kml file, so I setup two requests first:

NSString *server1URL = [NSString stringWithFormat:...];
NSMutableURLRe         


        
3条回答
  •  终归单人心
    2021-01-23 11:31

    Use GCD with sendSynchronousRequest: requests, they will be run in the background.

    Example:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        NSURLRequest *request = [NSURLRequest requestWithURL:url1];
        NSURLResponse *response;
        NSError *error;
        NSData *data1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        // do something with the data
    });
    
    dispatch_async(queue, ^{
        NSURLRequest *request = [NSURLRequest requestWithURL:url2];
        NSURLResponse *response;
        NSError *error;
        NSData *data2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        // do something with the data
    });
    

提交回复
热议问题