Android-DDP icon indicating copy to clipboard operation
Android-DDP copied to clipboard

destroyInstance() breaks if not connected

Open ryanw-mobile opened this issue 8 years ago • 2 comments

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!

ryanw-mobile avatar Apr 22 '17 17:04 ryanw-mobile

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?

ocram avatar Apr 23 '17 21:04 ocram

Yes, I agree with that.

ryanw-mobile avatar Apr 24 '17 01:04 ryanw-mobile