Francesco Galgani
Francesco Galgani

Reputation: 6249

Codename One EasyThread implementation that repeats a runnable if its result is false

Note for the readers: this question is specific for Codename One only.

I'm developing an app that needs some initial data from a server to run properly. The first shown Form doesn't need this data and there is also a splash screen on the first run, so if the Internet connection is good there is enought time to retrive the data... but the Internet connection can be slow or absent.

I have in the init a call to this method:

private void getStartData() {
    Runnable getBootData = () -> {
        if (serverAPI.getSomething() && serverAPI.getXXX() && ...) {
            isAllDataFetched = true;
        } else {
            Log.p("Connection ERROR in fetching initial data");
        }
    };
    EasyThread appInfo = EasyThread.start("APPINFO");
    appInfo.run(getBootData);
}

Each serverAPI method in this example is a synchronous method that return true if success, false otherwise. My question is how to change this EasyThread to repeat again all the calls to (serverAPI.getSomething() && serverAPI.getXXX() && ...) after one second if the result is false, and again after another second and so on, until the result is true.

I don't want to shown an error or an alert to the user: I'll show an alert only if the static boolean isAllDataFetched is false when the requested data is strictly necessary.

I tried to read carefully the documentation of EasyThread and of Runnable, but I didn't understand how to handle this use case.

Upvotes: 2

Views: 68

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

Since this is a thread you could easily use Thread.sleep(1000) or more simply Util.sleep(1000) which just swallows the InterruptedException. So something like this would work:

while(!isAllDataFetched) {
    if (serverAPI.getSomething() && serverAPI.getXXX() && ...) {
        isAllDataFetched = true;
    } else {
        Log.p("Connection ERROR in fetching initial data");
        Util.sleep(1000);
    }
}

Upvotes: 1

Related Questions