merveotesi
merveotesi

Reputation: 2183

Logic of data transfer on an Android application uses Observer/Observable

Explanation:

I have an Android application which has developed by a colleague of me.

I need to chop off the application to make the logic/programmatic part to reside on a server as a Web Service and to make the remaining part to reside as a client on Android device.

The problem:

As you can see in the below code, Observer/Observable system is used.

1) First, i need to understand, if the Observer/Observable system is used only to establish a communication between Observer(Activity) and Observable.

2) If so, should i put the Manager's programmatic code into the Web Service, and communicate with the Activity using HTTP GET, POST protocols.

3) Or have i misunderstand everything?

public class MainActivity extends Activity implements Observer {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        view = new MainView(this);

        ASensor aSensor;
        aSensor = new ASensor(MainActivity.this);

        aSensor.setListener(new Listener() {
            @Override
            public void onObjectDiscovered(List<AnObject> params) {
                List<AnObject> myParams = new ArrayList<AnObject>();

                TheManager.INSTANCE.feed(myParams);
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        if (observable instanceof TheManager) {
            List<AnObject> objectList = (List<AnObject>) data;
            String retValue = someProcessingMethod(objectList);
            view.setRetValue(retValue);
        }
    }
}

public class TheManager extends Observable implements Serializable {
    public static final TheManager INSTANCE = new TheManager();

    public void feed(List<AnObject> params) {
        List<AnObject> objectList = processParams(params);

        notifyObservers(objectList);
    }
}

Upvotes: 0

Views: 95

Answers (1)

JP Ventura
JP Ventura

Reputation: 5732

In Google I/O 2010, Virgil Dobjanschi presented the talk Android REST Client Applications Design Pattern, which solves problems of keeping local data consistent to a remote database using SyncAdapters.

If you choose to store your data using a ContentProvider and synchronize it through a SyncAdapter, you may find a detailed description at Google Developers of how to:

  1. Run the Sync Adapter When Server Data Changes
  2. Run the Sync Adapter When Content Provider Data Changes
  3. Run the Sync Adapter After a Network Message
  4. Run the Sync Adapter Periodically
  5. Run the Sync Adapter On Demand

Upvotes: 1

Related Questions