Skip to content
jedld edited this page Nov 9, 2012 · 3 revisions

AsyncTasks are used in android for tasks that are too lengthy to be executed in the UI thread. If you attempt to execute code which take too long to execute while under the UI Thread, your application will receive an ANR warning.

Async tasks are greatly simplified in droiuby. The convenience method async instantiates an async wrapper for you, where you can use the perform method to pass a block that will be executed asynchronously.

Note that it AsyncTasks are best suited to tasks that need to interact with the UI before or after the main task. If you only want to execute code on another thread and it doesn't need to modify Views then use the _thread function instead.

Example:

async.perform {
  http_get "asset:hello_world/_hello_world.xml"
}.done { |result|
  V('^mainLayout').inner = result
}.start

the output from the perform block will then be passed to done where you can perform operations that require it to be run under the UI thread (like modifying Views for example).