How to substitute Latitude's async example for a sync call

28 views
Skip to first unread message

BlazingFrog

unread,
Oct 28, 2011, 12:55:25 AM10/28/11
to Google APIs Client Library for Objective-C
I'm using the Latitude example (which works great) but the calls to
Google servers are done asynchronously when my work flow would favor
synchronous calls. Is there an easy way to go synchronous?

Thanks in advance.

David Phillip Oster

unread,
Oct 28, 2011, 11:58:45 AM10/28/11
to google-api-obj...@googlegroups.com
I'm presuming iOS, since Macs don't move around that much. The answer is NO. Synchronous network access gets you banned from the App Store for blocking the user interface, often for multiple seconds, when connecting via the cellphone network and DNS (domain name lookup) must be done.

The correct answer is: refactor your synchronous workflow so that the stuff you do after the latitude request can go in a block which will be executed after the data arrives.

Greg Robbins

unread,
Oct 31, 2011, 8:49:54 PM10/31/11
to google-api-obj...@googlegroups.com
Developers coming to Objective-C from other languages are often accustomed to creating separate threads to wait for tasks to finish. But as David points out, that style is discouraged in Cocoa applications. Creating threads leads to more error-prone application code, and blocking the main thread will hurt the device's responsiveness.

Instead, iOS and Mac apps can rely on the asynchronous calls provided by the operating system, along with the Objective-C blocks style of callback to keep the source code looking linear and straightforward. The library follows that design pattern as well. Application code can be written in a sequential style, but no new threads are needed, nor does any thread need to be blocked by synchronous calls.
Reply all
Reply to author
Forward
0 new messages