delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

Many onConnect events #123

Closed proft closed 7 years ago

proft commented 7 years ago

Hello!

Interesting stuff! Preface: I have Meteor app and Android app.

My onConnectmethod

public void onConnect(boolean signedInAutomatically) {
    Log.i("VVV", "CONNECTED!");
...

In log I see CONNECTED! every 10-15 seconds. Why? There is no new data, no problem with network connection ...

I get Meteor instance as following

In fragment

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (NetworkUtil.isNetwork(getActivity())) {
            meteor = NetworkUtil.getMeteor(getActivity());
            meteor.addCallback(this);
            meteor.connect();
        } else {
        }
    }

    public void onConnect(boolean signedInAutomatically) {
        Log.i("VVV", "CONNECTED!");
    }

In NetworkUtil

    public static Meteor getMeteor(Context context) {
        Meteor m;
        try {
            m = MeteorSingleton.getInstance();
        } catch (IllegalStateException e) {
            MeteorSingleton.createInstance(context, "ws://server.com/websocket", new InMemoryDatabase());
            m = MeteorSingleton.getInstance();
        }
        return m;
    }

To my mind, it should be only one CONNECTED!. Am I right?

ocram commented 7 years ago

That looks like you are at least establishing a new connection every time you open a new Activity. You should call the connect method on a Meteor instance only once. But you are doing it in onActivityCreated which means that it's done more often.

Try removing that meteor.connect() call in onActivityCreated and adding it as m.connect() at the end of your catch block in getMeteor instead.

proft commented 7 years ago

Thank you for response!

I see you point of view but ... I just open emulator and wait (emulator and activity is in focus). No Activity recreation. So method onActivityCreated doesn't call again.

I can't add m.connect() at the end of catch block in getMeteor because getMeteor is in utility class. All actions occur in ListFragment and method like onConnect, onDisconnect, onDestroy are in this ListFragment.

So I have one utility method with MeteorSingleton and many fragments that use meteor object from NetworkUtiland in this fragments I created methods for onConnect, onDisconnect, onDestroy. To my surprise each ListFragment call onConnect on every 10-15 secs when this fragment is in focus.

ocram commented 7 years ago

After

MeteorSingleton.createInstance(context, "ws://server.com/websocket", new InMemoryDatabase());
m = MeteorSingleton.getInstance();

can you add

m.setLoggingEnabled(true);

and check if there's some helpful information printed to the console then?

Reading your explanation, I still don't understand why you can't move the call to the connect method as explained. Did you actually try this?

proft commented 7 years ago

I rearranged connect method and I got what I want :) By the way, don't know about setLoggingEnabled. Thanks for your patience! :)