how to use sendAsynchronousRequest:queue:completionHandler:

前端 未结 6 1655
你的背包
你的背包 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:15

    I have been working on a similar problem, I posted this question and got a clear answer here, I hope that helps with Part 2.

    For part 1 what the others mentioned here are good but you need to add another check (I have modified an answer below). It is possible that your request will return say a 404 Error (page not found) in which case you will not get and error and data will be > 0. The 200 is a good response, you could also check the StatusCode for 404 or whatever.

         [NSURLConnection
         sendAsynchronousRequest:urlRequest
         queue:[[NSOperationQueue alloc] init]
         completionHandler:^(NSURLResponse *response,
                             NSData *data,
                             NSError *error) 
        {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if ([data length] >0 && error == nil && [httpResponse statusCode] == 200)
         {
    
                        // DO YOUR WORK HERE
    
         }
    

提交回复
热议问题