5

What is synchronous and asynchronous in ios ? I am new in objective c. Which one i should use in my code while i am getting data from server. So please help me.

Thanks in advance.

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
  • Take a look at this : http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – Hugo Hilário Sep 09 '13 at 10:09
  • Async is not specific to iOS. Async is explained well in any iOS books, and there are more than one way to do it. Server communication must always be done asynchronously. – Khanh Nguyen Sep 09 '13 at 10:11

5 Answers5

6

You should always use asynchronous loading of network requests.

Asynchronous never block the main thread waiting for a network response.

Asynchronous can be either synchronous on a separate thread, or scheduled in the run loop of any thread.

Synchronous blocks main thread until they complete request.

For Demo code or turorial have a look into this link Asynchronous web service client using NSURLConnection and SBJSON

abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
0

The majority of the time you will go for asynchronous calls for that kind of operations, otherwise you're UI will block because you are using the main thread.

Hugo Hilário
  • 2,848
  • 2
  • 27
  • 43
0

Synchronous, as the name suggests the action will happen in synchronous with the run loop of your application.

To understand it better, say you have to display some data in UITableview after fetching the data from server.Imagine that the request and response from server takes like 3 seconds. When you are fetching this data synchronously from the server, your app will freeze for like 3 seconds between loading tableview and loading the data contents into that tableview

Now if you are sending your request asynchronously, your app won't freeze but it will load the tableview and tableview contents before the server can respond. In other words, your app won't wait for the 3 second of server response time.You have to take necessary delegate actions or blocks actions to check the response and reload the tabledata so that the server response is displayed in tableview.

Which method is better is pure choice what the developer wants and their app should behave but Apple documentation recommends if you are using synchronous calls do not initiate the call from current run loop.

slysid
  • 5,236
  • 7
  • 36
  • 59
0

Using asynchronous all threads are execute the operations parallel. So, Never block the main thread waiting for a network response.

Using synchronous all threads are execute the operations one by one. so, should wait until the other thread task done.

Hope It will be suitable.

0

Quick note based on other answers: dispatch_sync will not block the main thread unless you dispatch to the main thread.

Example:

// Block main thread because the main queue is on it.
dispatch_sync(dispatch_get_main_queue(), ^{ /*do stuff*/ });

// Block background thread.
dispatch_sync(my_work_queue, ^{ /*do stuff*/ });

A Synchronous call(blocking) is one that has to be completed before subsequent calls can be run in the same queue. It is given all of the processor time for that queue until it is complete. This makes it block the queue. Asynchronous calls can be started in a queue, and then left running on another thread(processor time schedule), owned by that queue, while other calls are started with other threads.

It is very important to use dispatch_async for web calls because it may take time to get a result back and you want other tasks to be able to start in the queue and use it's threads. A common practice is to do web work, like downloading a file, on a custom background queue and then dispatch to the main queue when it is complete, to update the user.

There is more to this and you can read about dispatch queues from Apple, here.

Eric Mentele
  • 864
  • 9
  • 16