Skip to content

Commit

Permalink
异步存储,避免阻塞主线程
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Oct 8, 2021
1 parent 5ed15bd commit b4cd126
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
1 change: 1 addition & 0 deletions accountcache/consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ public static java.lang.String TABLENAME;
-keep class rx.** { *; }
-keep class com.facebook.flipper.** { *; }
-keep class com.facebook.jni.** { *; }
-keep @org.greenrobot.greendao.annotation.Entity class * {*;}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Base64;
import android.widget.Toast;
Expand Down Expand Up @@ -95,9 +97,19 @@ public static void selectAccount(int hostType, FragmentActivity activity, String

@Override
public void onGranted(List<String> permissions, boolean all) {
List<DebugAccount> accounts = MyDbUtil.getAll(hostType, countryCode);
new Thread(new Runnable() {
@Override
public void run() {
List<DebugAccount> accounts = MyDbUtil.getAll(hostType, countryCode);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
showSelectAccountDialog(hostType, activity, countryCode, accounts, callback);
}
});
}
}).start();

showSelectAccountDialog(hostType, activity, countryCode, accounts, callback);
}

@Override
Expand Down Expand Up @@ -207,29 +219,32 @@ public static void saveAccount(Activity activity, int currentHostType, final Str

@Override
public void onGranted(List<String> permissions, boolean all) {


List<DebugAccount> list = MyDbUtil.getDaoSession().getDebugAccountDao()
.queryBuilder().where(DebugAccountDao.Properties.Account.eq(account)
, DebugAccountDao.Properties.CountryCode.eq(countryCode)
, DebugAccountDao.Properties.HostType.eq(currentHostType)).list();
if (list == null || list.isEmpty()) {
DebugAccount debugAccount = new DebugAccount();
debugAccount.account = account;
debugAccount.pw = pw;
debugAccount.updateTime = System.currentTimeMillis();
debugAccount.position = 0;
debugAccount.countryCode = countryCode;
debugAccount.hostType = currentHostType;
debugAccount.usedNum = 1;
MyDbUtil.getDaoSession().getDebugAccountDao().insert(debugAccount);
} else {
DebugAccount debugAccount = list.get(0);
debugAccount.usedNum = debugAccount.usedNum + 1;
debugAccount.pw = pw;
debugAccount.updateTime = System.currentTimeMillis();
MyDbUtil.getDaoSession().getDebugAccountDao().update(debugAccount);
}
new Thread(new Runnable() {
@Override
public void run() {
List<DebugAccount> list = MyDbUtil.getDaoSession().getDebugAccountDao()
.queryBuilder().where(DebugAccountDao.Properties.Account.eq(account)
, DebugAccountDao.Properties.CountryCode.eq(countryCode)
, DebugAccountDao.Properties.HostType.eq(currentHostType)).list();
if (list == null || list.isEmpty()) {
DebugAccount debugAccount = new DebugAccount();
debugAccount.account = account;
debugAccount.pw = pw;
debugAccount.updateTime = System.currentTimeMillis();
debugAccount.position = 0;
debugAccount.countryCode = countryCode;
debugAccount.hostType = currentHostType;
debugAccount.usedNum = 1;
MyDbUtil.getDaoSession().getDebugAccountDao().insert(debugAccount);
} else {
DebugAccount debugAccount = list.get(0);
debugAccount.usedNum = debugAccount.usedNum + 1;
debugAccount.pw = pw;
debugAccount.updateTime = System.currentTimeMillis();
MyDbUtil.getDaoSession().getDebugAccountDao().update(debugAccount);
}
}
}).start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ private static void initGreenDao(Application context) {
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
//兼容旧数据迁移情况
moveOldData(context);
new Thread(new Runnable() {
@Override
public void run() {
moveOldData(context);
}
}).start();

}

private static void moveOldData(Application context) {
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(path: ':accountcache')
//debugImplementation 'com.github.hss01248.accountCacher:accountcache:1.1.1'
//implementation project(path: ':no-op')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand Down

0 comments on commit b4cd126

Please sign in to comment.