Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

n-1 async callback implementation

I'm re writing an IOS app in android. It's a task I've set to try and learn android. In android I've being learning about different async messaging options. What I have found so far are:

Callbacks
Broadcasts
Message Handlers

I'm trying to decide which approach is best to suit my purposes. In my IOS application I have 10 screen objects and 1 coordinator object. This is my n-1.

The way my ios currently works is my screen object calls a work method in my coordinator passing itself as a delegate. The coordinator performs some async work and calls various methods on the delegate when the job is done : completed / failed with reason etc.

Multiple screen objects can request work to be done at the same time.

So far my feeling that the callback / message handler approaches in android is more like a 1-1 relationship.

I'm leaning towards using the local broadcast manager. This does seem more like NSNotification than delegate methods but seems made for n-1 relationships.

Is the broadcast manager the best way to achieve n - 1 async work ?

Also are my assumption about the 1-1 relationship of callbacks and handlers correct?

like image 722
dubbeat Avatar asked Nov 23 '12 09:11

dubbeat


1 Answers

You can indeed use the broadcasts like NSNotification, but I would generally use broadcasts to message between different parts of my App, for example to communicate between a Service and an Activity, rather than within a specific part.

I don't see why you can't do exactly what you do in iOS on android as well. You would have a protocol in iOS defining what functions to call, and you can do the same in Java/Android by using interfaces.

In iOS you would have something like:

doStuffWithObject:(NSObject<SpecialStuff> *)object {}

while in Java you would have:

doStuffWithObject(SpecialStuff object) {}

with SpecialStuff being your protocol or interface. Because you don't have performSelectorOnBackground in android its a bit more work. Either use Timers, perhaps a separate Thread in combination with Handlers or use ASyncTask depending on what serves you best and how big the asynchronous task is.

ASyncTask is definitely worth looking into.


You could of course also make use of Observer and Observable.

A simple example with an handler and a timer that notifies its listeners of a new time every second (note that the handler is created on the main thread in this case, such that you can send messages back similar to using performSelectorOnMainThread in iOS):

    class SomeExample {

    private final ArrayList<TimeListener> timeListeners;

    private final Handler handler = new Handler();
    private final TimeUpdateRunnable timeUpdateRunnable = new TimeUpdateRunnable();

    public SomeExampleView {

        timeListeners = new ArrayList<TimeListener>();

        updateTimer = new Timer("General Update Timer");

        TimeUpdateTask timeUpdateTask = new TimeUpdateTask();
        updateTimer.scheduleAtFixedRate(timeUpdateTask, (60 * 1000) -   (System.currentTimeMillis() % (60 * 1000)), 60 * 1000);
    }

    public void addTimeListener(TimeListener timeListener) {
        timeListeners.add(timeListener);
    }

    public boolean removeTimeListener(TimeListener timeListener) {
        return timeListeners.remove(timeListener);
    }


    class TimeUpdateTask extends TimerTask {
        public void run() {
            handler.post(timeUpdateRunnable);
        }
    }

    private class TimeUpdateRunnable implements Runnable {
        public void run() {
            for (TimeListener timeListener : timeListeners) {
                timeListener.onTimeUpdate(System.currentTimeMillis());
            }
        }
    }
}

And my listener interface which is similar to Observer

public interface TimeListener {
    void onTimeUpdate(long time);
}
like image 65
MrJre Avatar answered Nov 09 '22 03:11

MrJre