Module: MotionAsync
- Defined in:
- lib/motion-async/motion_async.rb,
lib/motion-async/version.rb
Overview
MotionAsync provides a friendly Ruby wrapper around Android’s AsyncTask.
You can call it directly:
Or include the module to make the async command available wherever you need it
Usage:
Run a block of code in the background:
Specify a block to execute when the operation completes. The return value of the async block is passed in as a parameter:
Alternate syntax for the same example:
For progress updates, provide a :progress block, and periodically call #progress on the task object in the :background block. The :progress block is executed on the main thread.
:pre_execute is invoked before the async operation begins and :cancelled is called if the task is cancelled.
async returns a reference to the task object (a subclass of AsyncTask); you can hold on to this in case you want to cancel it later. You can see if a task has been cancelled by calling cancelled?
end … def on_stop
@async_task.cancel
end
Delaying execution
The #after method works just like #async, but takes a float as its first parameter to specify the number of seconds to delay before executing the async block.
after(2) do
p "We did this 2 seconds later"
end
This works fine for relatively short delays (a few seconds at most), but you’d probably want to use a Handler for anything longer.
Constant Summary collapse
- VERSION =
"0.6"
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.after(delay, options = {}, &block) ⇒ Object
110 111 112 |
# File 'lib/motion-async/motion_async.rb', line 110 def self.after(delay, ={}, &block) MotionAsync.async(.merge(delay: delay), &block) end |
.async(options = {}, &block) ⇒ Object
104 105 106 107 108 |
# File 'lib/motion-async/motion_async.rb', line 104 def self.async(={}, &block) MotionAsyncTask.create(, &block).tap do |task| task.execute [] end end |
Instance Method Details
#after(delay, options = {}, &block) ⇒ Object
118 119 120 |
# File 'lib/motion-async/motion_async.rb', line 118 def after(delay, ={}, &block) MotionAsync.after(delay, , &block) end |
#async(options = {}, &block) ⇒ Object
114 115 116 |
# File 'lib/motion-async/motion_async.rb', line 114 def async(={}, &block) MotionAsync.async(, &block) end |