README.md in elevate-0.4.0 vs README.md in elevate-0.5.0

- old
+ new

@@ -1,10 +1,12 @@ -Elevate [![Code Climate](https://codeclimate.com/github/mattgreen/elevate.png)](https://codeclimate.com/github/mattgreen/elevate) +Elevate ====== Stop scattering your domain logic across your view controller. Consolidate it to a single conceptual unit with Elevate. +[![Code Climate](https://codeclimate.com/github/mattgreen/elevate.png)](https://codeclimate.com/github/mattgreen/elevate) [![Travis](https://api.travis-ci.org/mattgreen/elevate.png)](https://travis-ci.org/mattgreen/elevate) + Example ------- ```ruby @login_task = async username: username.text, password: password.text do @@ -17,20 +19,28 @@ credentials = API.login(@username, @password) if credentials UserRegistration.store(credentials.username, credentials.token) end - # Return value of block is passed back to on_completed + # Anything yielded from this block is passed to on_update + yield "Logged in!" + + # Return value of block is passed back to on_finish credentials != nil end - on_started do + on_start do # This block runs on the UI thread after the operation has been queued. SVProgressHUD.showWithStatus("Logging In...") end - on_completed do |result, exception| + on_update do |status| + # This block runs on the UI thread with anything the task yields + puts status + end + + on_finish do |result, exception| # This block runs on the UI thread after the task block has finished. SVProgressHUD.dismiss if exception == nil if result @@ -69,11 +79,11 @@ Installation ------------ Update your Gemfile: - gem "elevate", "~> 0.4.0" + gem "elevate", "~> 0.5.0" Bundle: $ bundle install @@ -89,24 +99,29 @@ Launch an async task with the `async` method: * Pass all the data the task needs to operate (such as credentials or search terms) in to the `async` method. * Define a block that contains a `task` block. The `task` block should contain all of your non-UI code. It will be run on a background thread. Any data passed into the `async` method will be available as instance variables, keyed by the provided hash key. -* Optionally, define `on_started` and `on_completed` blocks to run as the task starts and finishes. These are run in the UI thread, and should contain all of your UI code. +* Optionally: + * Define an `on_start` block to be run when the task starts + * Define an `on_finish` block to be run when the task finishes + * Define an `on_update` block to be called any time the task calls yield (useful for relaying status information back during long operations) +All of the `on_` blocks are called on the UI thread. `on_start` is guaranteed to precede `on_update` and `on_finish`. + ```ruby @track_task = async artist: searchBar.text do task do artist = API.track(@artist) ArtistDB.update(artist) end - on_started do + on_start do SVProgressHUD.showWithStatus("Adding...") end - on_completed do |result, exception| + on_finish do |result, exception| SVProgressHUD.dismiss end end ``` @@ -115,15 +130,13 @@ **NOTE: Within tasks, do not access the UI or containing view controller! It is extremely dangerous to do so. You must pass data into the `async` method to use it safely.** To Do ----- * Need ability to set timeout for tasks -* More thought on the semantics Caveats --------- * Must use Elevate's HTTP client instead of other iOS networking libs -* No way to report progress (idea: `execute` could yield status information via optional block) Inspiration ----------- * [Hexagonal Architecture](http://alistair.cockburn.us/Hexagonal+architecture) * [Android SDK's AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html)