how to use sendAsynchronousRequest:queue:completionHandler:

前端 未结 6 1664
你的背包
你的背包 2020-11-27 10:30

Two part question

Part one: I am trying to create an ASynchronous request to my database. I am currently doing it Synchronously however I want to le

6条回答
  •  情话喂你
    2020-11-27 11:34

    Here is a sample:

    NSString *urlAsString = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAsString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    
    
    [NSURLConnection
             sendAsynchronousRequest:urlRequest
             queue:[[NSOperationQueue alloc] init]
             completionHandler:^(NSURLResponse *response,
                                 NSData *data,
                                 NSError *error) 
            {
    
             if ([data length] >0 && error == nil)
             {
    
                            // DO YOUR WORK HERE
    
             }
             else if ([data length] == 0 && error == nil)
             {
                 NSLog(@"Nothing was downloaded.");
             }
             else if (error != nil){
                 NSLog(@"Error = %@", error);
             }
    
         }];
    

提交回复
热议问题