How do you prevent the objective-c method from executing while waiting for the NSURL connection to complete?

Good. It may be a very simple question to ask a very obvious answer, but I am a little stuck on the way to solving this problem.

I am encoding an objective-c application using the iPhone SDK. I have some custom objects. These objects execute an NSURL request and send data to the web server, return data, and move.

My problem is that I have an initial request to the server on which I authenticate. If successful, this request returns some information for the session, which I will use in the next request. If I want my application to run for authentication and then execute the initial subsequent request, I need the data from the first to execute the second. My problem is that in objective-c, when I send a message to the request object to execute the request and return the data to the variable as a result; the language, by its nature, continues to execute my method while the first request is being executed. My second request can never succeed because the first one did not return data.

I don’t remember what it is called, but it’s something in the line of procedure code, single-threaded code or parallel processing, or something like that.

What is the best way to spend my method execution until the object sending the web request receives the return value?

I'm sure there are several ways to do this, but I'm looking for what will be considered "right."

Example sudo code below.

//First Request is sent obtaining some data that we will soon manipulate MyClient *client = [[MyClient alloc] init]; NSString *returnData = [MyClient runRequest]; //Call an object that now plays with the data and does some neat things CustomObject *CustomObj = [[CustomObject alloc] init]; [CustomObj dealWithResponse:returnData]; //Problem is now when I process the return data, it hasn't completed the first request from line 2 yet 
+4
source share
2 answers

You are talking about "synchronous" and "asynchronous" operations. Synchronous operations are those that occur in step with your call code. Most of Cocoa works this way: you send a message to an object, say, to format a string, etc., and by the time the line of code is completed, the operation is complete.

But in the real world, some operations take longer than “instantaneous” ones (some intense graphics work, but mostly high or variable delays, such as disk I / O or, worse, network connectivity). These operations are unpredictable, and if the code is blocked until completion, it can be blocked indefinitely or forever, and that is not good.

So, how do we deal with this, we need to configure “callbacks” - you say: “Leave and perform this operation, and when you are done, call this other function.” Then inside this “callback” function, you start the second operation, which depends on the first. This way you don't spin in circles, waiting, you just get called “asynchronously” when each task is completed.

NSURLConnection (like other high latency objects) allows you to follow this pattern. When you create a connection, you configure delegate , and the delegate is called by certain methods when the resulting data is available. That way, you can serialize your operations if the delegate method starts the second operation only after the first is complete.

+4
source

I spent some extra time looking at the NSURLConnection documentation and found an instance method that allows sending synchronous requests.

Ben's answers and comments on programming concepts are perfectly correct and should be considered a true answer to the question. However, for those who really want to send a synchronous request and block the execution of your application until an answer appears, you can use the following method.

 sendSynchronousRequest:returningResponse:error: 

This can be found in the Apple class documentation for NSURLConnection

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendSynchronousRequest:returningResponse:error :

0
source

Source: https://habr.com/ru/post/1300365/


All Articles