fix assert: move disconnect into own thread

It's sometimes on the UI thread and so since it makes blocking calls
needs to be elsewhere. Give it its own thread.
This commit is contained in:
Eric House 2022-05-16 21:46:49 -07:00
parent 99e4905a68
commit 23241400f0

View file

@ -64,7 +64,7 @@ public class MQTTUtils extends Thread
private static String sLastRev = null; private static String sLastRev = null;
private MqttAsyncClient mClient; private MqttAsyncClient mClient;
private String mDevID; private final String mDevID;
private String[] mTopics = { null, null }; private String[] mTopics = { null, null };
private Context mContext; private Context mContext;
private MsgThread mMsgThread; private MsgThread mMsgThread;
@ -449,10 +449,21 @@ public class MQTTUtils extends Thread
if ( null == client ) { if ( null == client ) {
Log.e( TAG, "disconnect(): null client" ); Log.e( TAG, "disconnect(): null client" );
} else { } else {
// If we're on the UI thread we probably want to spawn a thread startDisconThread( client );
// for the blocking waitForCompletion() calls below }
Assert.assertTrueNR( false == Utils.isOnUIThread() );
// Make sure we don't need to call clearInstance(this)
synchronized ( sInstance ) {
Assert.assertTrueNR( sInstance[0] != this );
}
Log.d( TAG, "%H.disconnect() DONE", this );
}
private void startDisconThread( final MqttAsyncClient client )
{
new Thread( new Runnable() {
@Override
public void run() {
outer: outer:
for ( int ii = 0; ; ++ii ) { for ( int ii = 0; ; ++ii ) {
String action = null; String action = null;
@ -488,12 +499,7 @@ public class MQTTUtils extends Thread
} }
} }
} }
} ).start();
// Make sure we don't need to call clearInstance(this)
synchronized ( sInstance ) {
Assert.assertTrueNR( sInstance[0] != this );
}
Log.d( TAG, "%H.disconnect() DONE", this );
} }
private void clearInstance() private void clearInstance()