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

destroyInstance() breaks if not connected #133

Closed ryanw-mobile closed 1 year ago

ryanw-mobile commented 7 years ago

If it is already disconnected when calling MeteorSingleton.destroyInstance(), it will throw java.lang.IllegalStateException: You must have called the 'connect' method before you can disconnect again.

That means every time if I need to destroy the instance when disconnected, I have to reestablish the connection, just for destroyinstance() to disconnect it, which sounds a bit strange to me.

    public synchronized static void destroyInstance() {
        if (mInstance == null) {
            throw new IllegalStateException("Please call 'createInstance(...)' first");
        }

        mInstance.disconnect();
        mInstance.removeCallbacks();
        mInstance = null;
    }

Could the IllegalStateException be handled internally within destroyinstance(), or is it possible to have a way to simply set minstance = null? Thanks!

ocram commented 7 years ago

Thank you!

You're right, we should probably just wrap the statement

mInstance.disconnect();

in a try block and ignore the IllegalStateException like this:

try {
    mInstance.disconnect();
}
catch (IllegalStateException ignored) {}

That seems to match the goal of the MeteorSingleton class (which is simple management of the instance) and that of the destroyInstance method (which is to ensure that the instance is both disconnected and destroyed). Do you agree?

ryanw-mobile commented 7 years ago

Yes, I agree with that.