Giter VIP home page Giter VIP logo

android-zorn's Introduction

Android-Zorn

Asynchronous Workers and Worker Managers for Android.

Android Arsenal

How to use

simply fork or download the project, you can also download and create .aar file yourself.

Notable Features

  • use Worker API as a replacement for AsyncTask
  • workers run on background threads and return callbacks on the main(calling) thread.
  • automate a batch of workers with two worker managers:
    • PriorityWorkerManager - workers are processed according to their priority both serially or unbounded.
    • TopologicalWorkerManager - workers are processed according to a binary topological relation and order.

Using a Worker

1. Simply anonymously instantiate AbstractWorker

AbstractWorker worker = new AbstractWorker() {
    @Override
    protected void onProgress() {
        // runs on the calling/main thread
    }

    @Override
    protected void onComplete() {
        // runs on the calling/main thread
    }

    @Override
    public void work() {
        // here you put work to be done in a background thread
    }
};

// run async
worker.process();

2. Simply extend AbstractWorker

MyWorker worker = new MyWorker();

worker.process(new WorkerObserver() {
    @Override
    public void onWorkerComplete(IWorker worker) {
        // runs on the calling/main thread
    }

    @Override
    public void onWorkerProgress(IWorker worker) {
        // runs on the calling/main thread
    }

    @Override
    public void onWorkerError(IWorker worker) {
        // runs on the calling/main thread
    }
});

3. use SimpleWorker with a IWork object (like Runnable)

SimpleWorker sw = new SimpleWorker(new IWork() {
    @Override
    public void work() {
        // here you put work to be done in a background thread
    }
});

sw.process();

notes

  • IWorker.process(..) method also have an overloaded version where one can pass ExecutorService
  • IWorker supports many more methods and ideas. I did not go through all.
  • in the future, I will add support for Java native FutureTask and Callable so worker can have cancelling feature.

Using a Worker Manager

Worker managers support a lot of functionality such as pause, start, stop etc.. You can also implement a Worker manager using BaseAbstractWorkerManager, it is very easy. Contributions of new Worker managers are most welcome.

Priority Worker Manager.

pm = new PriorityWorkerManager("myId");
// serial mode
pm.setExecutionMode(AbstractWorkerManager.EXECUTION_MODE.SERIAL);

IWorker worker = null;
String  id;
int     priority;

for(int ix = 0; ix < 20; ix++) {
    id        = String.valueOf(ix);
    priority  = ix;
    
    worker    = new TestWorker(id, priority);

    pm.enqueue(worker);
}

pm.setListener(new WorkerManagerObserver() {
    @Override
    public void onComplete(IWorkerManager wm) {
        // runs on the calling/main thread
    }

    @Override
    public void onProgress(String id) {
        // runs on the calling/main thread
    }

    @Override
    public void onError(WorkerManagerErrorInfo err) {
        // runs on the calling/main thread
    }
});

pm.start();

Topological Worker Manager.

Use the TopologicalWorkerManager.Builder or Zorn.newTopologicalWorkerManager() to create a worker manager that takes into account a directed binary relation among workers.

TestWorker a1 = new TestWorker("a1");
TestWorker a2 = new TestWorker("a2");
TestWorker a3 = new TestWorker("a3");
TestWorker a4 = new TestWorker("a4");
TestWorker a5 = new TestWorker("a5");

TopologicalWorkerManager tm = new TopologicalWorkerManager.Builder().id("topological_test")
                                                          .listener(this)
                                                          .before(a1, a3)
                                                          .before(a2, a3)
                                                          .after(a4, a3)
                                                          .after(a5, a4)
                                                          .build();
                                                          
tm.setListener(...);

tm.start();                                                          

Dependencies

Terms

Contact Author

android-zorn's People

Contributors

hendrixstring avatar ravidsrk avatar

Watchers

James Cloos avatar Saad Bilal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.