AndroidGCD
Introduction: Using iOS-like Grand Central Dispatch (GCD) in Android.
Tags:
Using iOS-like Grand Central Dispatch (GCD) in Android.
Setup
Configure the dependency in Gradle
dependencies {
compile 'com.hl:android-gcd:0.2.0'
}
Usage
Add Async Task to Queue
Add task to UI queue
DispatchQueue.main().async(new Callable<Task<Void>>() {
@Override
public Task<Void> call() throws Exception {
// Add your UI task here
return null;
}
});
Add task to global background queue
DispatchQueue.global().async(new Callable<Task<Void>>() {
@Override
public Task<Void> call() throws Exception {
// Add your background task here
return null;
}
});
Add task to a specific backgroud queue
DispatchQueue.global(groupId).async(Callable);
Add Sync Task to Queue
// Run in UI queue
DispatchQueue.main().sync(Callable);
// Run in global background queue
DispatchQueue.global().sync(Callable);
// Run in specific background queue
DispatchQueue.global(groupId).sync(Callable);
Add Task with Delay
// Run in UI queue with delay
DispatchQueue.main().asyncDelayed(Callable, delayInMilliSeconds);
// Run in global background queue with delay
DispatchQueue.global().asyncDelayed(Callable, delayInMilliSeconds);
// Run in specific background queue with delay
DispatchQueue.global(groupId).asyncDelayed(Callable, delayInMilliSeconds);
Cancel Tasks in Queue
// Cancel all tasks in UI queue
DispatchQueue.main().cancel();
// Cancel all tasks in global background queue
DispatchQueue.global().cancel();
// Cancel all tasks in specific background queue
DispatchQueue.global(groupId).cancel();
Lincense
MIT License
Copyright (c) 2017 Herman Liang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
