Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async Android

Async Android

NYC Android Developers Meetup, June 2015

Jamie McDonald

June 18, 2015
Tweet

More Decks by Jamie McDonald

Other Decks in Programming

Transcript

  1. @Override public void onCreate() { super.onCreate(); Log.i(TAG, “Hi from the

    main thread!”); } Main Thread title, date, 01 of 10
  2. ANR title, date, 01 of 10 I/Choreographer﹕ Skipped 480 frames!

    The application may be doing too much work on its main thread.
  3. @UiThread public void updateUi(String title) { textView.setText(title); } @WorkerThread public

    void showTitle() { textView.setText(SlowStuff.loadTitle()); } Tools title, date, 01 of 10
  4. @UiThread public void updateUi(String title) { textView.setText(title); } @WorkerThread public

    void showTitle() { textView.setText(SlowStuff.loadTitle()); } Tools title, date, 01 of 10
  5. new Thread() { public void run() { final String result

    = SlowStuff.blocking(); new Handler().post(new Runnable() { @Override public void run() { updateUi(result); } }); } }.start(); Thread title, date, 01 of 10
  6. class LooperThread extends Thread { private Handler handler; public void

    run() { Looper.prepare(); handler = new Handler() { public void handleMessage(Message msg) { // Process messages here } }; Looper.loop(); } } Looper title, date, 01 of 10
  7. IntentService title, date, 01 of 10 public class BackgroundService extends

    IntentService { public BackgroundService() { super("BackgroundWork"); } @Override protected void onHandleIntent(Intent intent) { SlowStuff.blocking(); } } startService(new Intent(this, BackgroundService.class));
  8. new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params)

    { return SlowStuff.blocking(); } @Override protected void onPostExecute(String s) { updateUi(s); } }.execute(); AsyncTask title, date, 01 of 10
  9. new AsyncTask<Void, Void, Result>() { [...] @Override protected void onPostExecute(Result

    result) { if (result.isSuccess()) { doNextAsyncStep(result.getValue()); } else { // ??? } } }.execute(); Callback Hell title, date, 01 of 10
  10. class DataLoader extends AsyncTaskLoader<String> { public BackgroundLoader(Context context) { super(context);

    } @Override protected void onStartLoading() { forceLoad(); } @Override public String loadInBackground() { return SlowStuff.blocking(); } } Loader title, date, 01 of 10
  11. getLoaderManager().initLoader(LOADER_ID, null, new LoaderManager.LoaderCallbacks<String>() { @Override public Loader<String> onCreateLoader(int id,

    Bundle args) { return new DataLoader(MainActivity.this); } @Override public void onLoadFinished(Loader<String> loader, String text) { updateUi(text); } @Override public void onLoaderReset(Loader<String> loader) { loader.reset(); } }); Loader title, date, 01 of 10
  12. RxJava title, date, 01 of 10 “RxJava tries to be

    very lightweight. [...] focused on just the Observable abstraction and related higher-order functions”
  13. class MySubscriber extends DefaultSubscriber<String> { @Override public void onNext(String text)

    { updateUi(text); } @Override public void onError(Throwable e) { displayError(e); } } Subscriber title, date, 01 of 10
  14. title, date, 01 of 10 Thread IntentService AsyncTask Loader RxJava

    Main thread callbacks Configuration changes Error handling Composability Flexible scheduling
  15. Operators title, date, 01 of 10 flatMap map filter doOnNext

    onErrorResumeNext distinct cache zip retry ...
  16. public final class EventQueue { static final Queue<StateTransition> PLAYBACK_STATE; static

    final Queue<ProgressEvent> PLAYBACK_PROGRESS; static final Queue<PlayableEvent> PLAYABLE_CHANGED; [...] } Event Bus title, date, 01 of 10
  17. Subtitle Respond to state without storing it title, date, 01

    of 10 Observable.combineLatest( eventBus.queue(EventQueue.PLAY_QUEUE_TRACK), eventBus.queue(EventQueue.PLAYER_UI), combineStates) .doOnNext(setTrackHasBeenSeen)
  18. Downsides title, date, 01 of 10 Debugging Testing long call

    chains Learning curve Boilerplate Config changes Backpressure Where & when to use it?