Search code examples
codenameone

Codename One - ConnectionRequest debug


I have the following code in Codename One:

        Log.p("getPageMap,pre addToQueueAndWait");
        NetworkManager.getInstance().addToQueueAndWait(connectionRequest);
        Log.p("getPageMap,post addToQueueAndWait");

It executes mutiple times and retrieves data from the internet (https://api.beta.tab.com.au...).

When I run in the simulator, all is ok.

When I run on a physical phone, and monitor via logcat, two executions work ok, a third execution results in the first Log.p statement appearing, but the second Log.p statement does not appear. i.e. execution appears to "freeze" within addToQueueAndWait.

Is there further logging / debugging I can include to determine the cause of the problem?

Things I've tried: I did have a call to addNetworkErrorListener (but it did not appear to be triggered). I have overridden GZConnectionRequest's handleErrorResponseCode, handleException, postResponse and buildRequestBody with basic Log.p statements, but they don't seem to provide any useful information. Things that may impact: the website has recently changed to return gzipped json, but I think I've adjusted to this change accordingly. Cookies may be a factor - are they handled differently in the simulator vs device? Changing the priority of the CZConnectionRequest to PRIORITY_CRITICAL appears to have had a posivite impact. Should I move away from GZConnectionRequest and try an alternate approach?


Solution

  • That sounds like a deadlock or a race condition that you only see on the device due to the differences in performance. These things are notoriously hard to debug, especially on the device.

    I would recommend migrating the code to use addToQueue if possible. It's simpler as it removes the need to deal with thread blocking. The code is a bit harder since it's asynchronous and I can't give you a direct equivalent. Something like this should generally work:

    Log.p("getPageMap,pre addToQueue");
    connectionRequest.addResponseListener(networkEvent -> {
        Log.p("getPageMap,post addToQueue");
    });
    NetworkManager.getInstance().addToQueue(connectionRequest);
    

    Alternatively, ff you want to debug this you can do the following:

    • Log the current thread - this can help understand what's going on. You can even see this in the debugger.

    • Print the stack trace every time you reach the first line (to check for recursion). You can do that by throwing and catching an exception then using printStackTrace().

    • Use the APIs in NetworkManager to inspect impact. E.g. use updateThreadCount to higher/lower values (it defaults to 2) to see if this changes results. Use enumurateQueue() to list the content of the queue.