提交 cf57c648 编写于 作者: G guolindev

Solve the getDatabase called recursively problem when operating database in...

Solve the getDatabase called recursively problem when operating database in the DatabaseListener callback. issue https://github.com/LitePalFramework/LitePal/issues/528
上级 d354573d
......@@ -18,8 +18,11 @@ package org.litepal;
import org.litepal.exceptions.GlobalException;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
/**
* Base class of LitePal to make things easier when developers need to use
......@@ -44,8 +47,11 @@ public class LitePalApplication extends Application {
/**
* Global application context.
*/
@SuppressLint("StaticFieldLeak")
public static Context sContext;
public static Handler sHandler = new Handler(Looper.getMainLooper());
/**
* Construct of LitePalApplication. Initialize application context.
*/
......
......@@ -83,19 +83,29 @@ class LitePalOpenHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
Generator.create(db);
DatabaseListener listener = Operator.getDBListener();
final DatabaseListener listener = Operator.getDBListener();
if (listener != null) {
listener.onCreate();
LitePalApplication.sHandler.post(new Runnable() {
@Override
public void run() {
listener.onCreate();
}
});
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onUpgrade(SQLiteDatabase db, final int oldVersion, final int newVersion) {
Generator.upgrade(db);
SharedUtil.updateVersion(LitePalAttr.getInstance().getExtraKeyName(), newVersion);
DatabaseListener listener = Operator.getDBListener();
final DatabaseListener listener = Operator.getDBListener();
if (listener != null) {
listener.onUpgrade(oldVersion, newVersion);
LitePalApplication.sHandler.post(new Runnable() {
@Override
public void run() {
listener.onUpgrade(oldVersion, newVersion);
}
});
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册