This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SubscriptionActivity extends Activity { | |
Subscription subscription; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
subscription = getObservable() | |
.subscribeOn(Schedulers.io()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Here first of all, we get the list of users from server. | |
* Then for each userId from user, it makes the network call to get the detail | |
* of that user. | |
* Finally, we get the userDetail for the corresponding user one by one | |
*/ | |
/* | |
* This observable return the list of users. | |
*/ | |
private Observable<List<User>> getUserListObservable() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*-------------- Getting the userList ----------------*/ | |
RxAndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}") | |
.addPathParameter("pageNumber", "0") | |
.addQueryParameter("limit", "3") | |
.build() | |
.getParseObservable(new TypeToken<List<User>>() {}) // This returns you Observable | |
.subscribeOn(Schedulers.io()) // do the network call on another thread | |
.observeOn(AndroidSchedulers.mainThread()) // return the result in mainThread | |
.subscribe(new Observer<List<User>>() { | |
@Override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* do some task at high priority | |
*/ | |
public void doSomeTaskAtHighPriority(){ | |
DefaultExecutorSupplier.getInstance().forBackgroundTasks() | |
.submit(new PriorityRunnable(Priority.HIGH) { | |
@Override | |
public void run() { | |
// do some background work here at high priority. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DefaultExecutorSupplier{ | |
private final PriorityThreadPoolExecutor mForBackgroundTasks; | |
private DefaultExecutorSupplier() { | |
mForBackgroundTasks = new PriorityThreadPoolExecutor( | |
NUMBER_OF_CORES * 2, | |
NUMBER_OF_CORES * 2, | |
60L, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PriorityRunnable implements Runnable { | |
private final Priority priority; | |
public PriorityRunnable(Priority priority) { | |
this.priority = priority; | |
} | |
@Override | |
public void run() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Get the future of the task by submitting it to the pool | |
*/ | |
Future future = DefaultExecutorSupplier.getInstance().forBackgroundTasks() | |
.submit(new Runnable() { | |
@Override | |
public void run() { | |
// do some background work here. | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainThreadExecutor implements Executor { | |
private final Handler handler = new Handler(Looper.getMainLooper()); | |
@Override | |
public void execute(Runnable runnable) { | |
handler.post(runnable); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PriorityThreadFactory implements ThreadFactory { | |
private final int mThreadPriority; | |
public PriorityThreadFactory(int threadPriority) { | |
mThreadPriority = threadPriority; | |
} | |
@Override | |
public Thread newThread(final Runnable runnable) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Using it for Background Tasks | |
*/ | |
public void doSomeBackgroundWork(){ | |
DefaultExecutorSupplier.getInstance().forBackgroundTasks() | |
.execute(new Runnable() { | |
@Override | |
public void run() { | |
// do some background work here. | |
} |