GCX-HCI / tray

a SharedPreferences replacement for Android with multiprocess support
Apache License 2.0
2.29k stars 273 forks source link

How to upgrade data? #126

Open jinfatimay opened 6 years ago

jinfatimay commented 6 years ago

tray Version

0.12.0

How have you setup tray

Initialized in Application.onCreate

Device(s)

too many (samsung, huawei, google, etc.)

Android Version

from 4.4 to 7

Description

I traced the code. Tray will upgrade version in constructor (it will call isVersionChangeChecked()).

In isVersionChangeChecked(), it will check version(changeVersion()) to do onCreate/onUpgrade/onDowngrade if mChangeVersionSucceeded == false.

Notice that the variable mChangeVersionSucceeded is always false until the last line in changeVersion() (after finishing onCreate/onUpgrade/onDowngrade) And the new version value (getStorage().setVersion(newVersion)) is also set after finishing onCreate/onUpgrade/onDowngrade

Assume I want to toggle a boolean value in new version. I write the code like ...

@Override
protected void onUpgrade(int oldVersion, int newVersion) {
        put(key, !getBoolean(key, false));
}

However, at the beginning of put method is...

if (!isVersionChangeChecked()) {
     return false;
}

It calls isVersionChangeChecked() again! At that time, mChangeVersionSucceeded is still false and getStorage().getVersion() is also the old version. So...it will passes all IF statements then calls onUpgrade again and again.

Please teach me how to fix this issue? 😃 Sorry, I'm not good at English. 😞

jinfatimay commented 6 years ago

I found a way... I can use storage directly to save the value I want to upgrade. Is that a good way..?

Polyterative commented 5 years ago

I guess it is