JakeWharton / ThreeTenABP

An adaptation of the JSR-310 backport for Android.
Apache License 2.0
3.55k stars 132 forks source link

Investigate lazily loading DB #23

Open JakeWharton opened 8 years ago

kenyee commented 8 years ago

I was performance tweaking this weekend and trying to figure out how to cut this time down too...I'm seeing around 600ms for this init if done inside the Application class.

Maybe a best practice suggestion on how to initialize this safely on a background thread would be enough? Been trying to figure out how to do this w/o hitting any race conditions but don't see how to do it because all the other classes just use stuff in the library. Can't put it inside a splash page because Android might restore the app into another Activity so the only central location before anything runs is the Application class :-(

AwesomelyAmazing commented 7 years ago

Is this issue still relevant? I also stumbled upon StrictMode policy violation the other day.

Is something like this:


public final class AndroidThreeTenAsync {

    private static final AtomicBoolean INITIALIZED = new AtomicBoolean();

    public static void init(final Context context) {
        if (INITIALIZED.getAndSet(true)) {
            return;
        }
        new Thread(() -> {
            synchronized (ZoneRulesProvider.class) {
                AndroidThreeTen.init(context);
            }
        }).start();
    }
}

a viable solution? It still has a delay if application was restored on activity that uses the library, but in most of the cases it just loads DB in the background.

JakeWharton commented 7 years ago

No, that's inherently racy.

On Tue, Jan 31, 2017 at 1:22 AM Aleksandrs Orlovs notifications@github.com wrote:

Is this issue still relevant? I also stumbled upon StrictMode policy violation the other day.

Is something like this:

public final class AndroidThreeTenAsync {

private static final AtomicBoolean INITIALIZED = new AtomicBoolean();

public static void init(final Context context) {
    if (INITIALIZED.getAndSet(true)) {
        return;
    }
    new Thread(() -> {
        synchronized (ZoneRulesProvider.class) {
            AndroidThreeTen.init(context);
        }
    }).start();
}

}

a viable solution? It still has a delay if application was restored on activity that uses the library, but in most of the cases it just loads DB in the background.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JakeWharton/ThreeTenABP/issues/23#issuecomment-276284776, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEWxyFfTE1RkUIAOPfQoYoRJxW0-aks5rXtMPgaJpZM4HyMTH .

matejdro commented 5 years ago

How is this related to https://github.com/JakeWharton/ThreeTenABP/commit/fd0863ea5f0e75b0262cbad76d69d1356fa9a236?

If I understand the description, it actually makes TZ loading lazy (e.g. TZ only gets loaded when it is needed)?