Bolts 是一款底层类库集合, 在后台实现异步操作, 并提供接口反馈当前异步执行的程度 (可以通过接口实现UI进度更新), 最后反馈执行的结果给UI主线程, 与AsyncTask比较: (1)使用的是无大小限制的线程池; (2)任务可组合可级联,防止了代码耦合。
使用
dependencies { compile 'com.parse.bolts:bolts-tasks:1.4.0' compile 'com.parse.bolts:bolts-applinks:1.4.0'}
示例代码
/** Gets a String asynchronously. */ public Task<String> getStringAsync() { // Let's suppose getIntAsync() returns a Task<Integer>. return getIntAsync().continueWith( // This Continuation is a function which takes an Integer as input, // and provides a String as output. It must take an Integer because // that's what was returned from the previous Task. new Continuation<Integer, String>() { // The Task getIntAsync() returned is passed to "then" for convenience. public String then(Task<Integer> task) throws Exception { Integer number = task.getResult(); return String.format("%d", Locale.US, number); } } ); }