N-1 implementation of asynchronous callback

I am writing an iOS application on Android. This is the task I set to try and learn Android. In android, I am exploring various asynchronous messaging options. So far I have found the following:

Callbacks Broadcasts Message Handlers 

I am trying to decide which approach is best for my purposes. In my iOS app, I have 10 display objects and 1 coordinator object. This is my n-1.

How does my ios work, my display object calls the working method in my coordinator, passing myself as a delegate. The coordinator does some asynchronous work and calls various methods for the delegate when the task is completed: completed / failed, etc.

Several display objects may request work to be performed simultaneously.

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

I tend to use a local broadcast manager. This is more like NSNotification than delegation methods, but seems to be done for n-1 relationships.

Is broadcast manager the best way to achieve n-1 asynchronous operation?

Is my assumption about the ratio of callbacks and handlers 1-1 also true?

+6
source share
1 answer

You can really use broadcasts such as NSNotification, but I usually used broadcasts to communicate between different parts of my application, for example, to communicate between the Service and Activity, and not with a specific part.

I do not understand why you cannot do what you do in iOS on Android. You will have a protocol in iOS defining which functions to call, and you can do the same in Java / Android using interfaces.

In iOS, you have something like:

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

while in Java you would:

 doStuffWithObject(SpecialStuff object) {} 

when SpecialStuff is your protocol or interface. Since you don't have performSelectorOnBackground in android, this is a bit more. Use Timers, perhaps a separate Thread in combination with Handlers, or use ASyncTask depending on what suits you best and how large the asynchronous task is.

ASyncTask is definitely worth a look.


Of course, you can also use Observer and Observable .

A simple example with a handler and a timer that notifies its listeners of a new time every second (note that the handler is created in the main thread in this case, so 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); } 
+2
source

All Articles