Managing two NSURLConnection

前端 未结 3 467
终归单人心
终归单人心 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:25

    How about assigning tags to each connection and checking the tags via an if/else or switch in the connectionDidFinishLoading?

    0 讨论(0)
  • 2021-01-23 11:29
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
        if( [connection isEqual: AConnection] ){
            // do connection A stuff
        }
        else if( [connection isEqual: BConnection] ){
            // do connection B stuff
        }  
    }
    
    0 讨论(0)
  • 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
    });
    
    0 讨论(0)
提交回复
热议问题