Android DBFlow 不保存对象
Android DBFlow does not save objects
我有这样一个数据库:
@Database(name = QuestionDatabase.NAME, version = QuestionDatabase.VERSION)
public class QuestionDatabase {
public static final String NAME = "QuestionDatabase"; // we will add the .db extension
public static final int VERSION = 1;
}
和这样的 table:
@Table(database = QuestionDatabase.class)
public class Question extends BaseModel {
@PrimaryKey
public int localID;
@Column
public int Id;
@Column
public String Answer;
@Column
public String ImageURL;
@Column
public boolean IsFavorite;
@Column
public boolean IsSolved;
}
以及从服务器检索数据的异步任务:
public class QuestionRetriever extends AsyncTask<Integer, Void, Integer> {
private Activity callerActivity;
private QuestionAdapter questionsAdapter;
private List<Question> callerQuestions;
private Integer pageSize = 10;
public QuestionRetriever(Activity callerActivity, QuestionAdapter questionsAdapter, List<Question> questions){
this.callerActivity = callerActivity;
this.questionsAdapter = questionsAdapter;
this.callerQuestions = questions;
}
@Override
protected Integer doInBackground(Integer... pageNumbers) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.33:313/")
.addConverterFactory(GsonConverterFactory.create())
.build();
QuestionWebService service = retrofit.create(QuestionWebService.class);
Call<List<Question>> call = service.getQuestionsPaged(pageNumbers[0].toString(), pageSize.toString());
try {
Response<List<Question>> excecuted = call.execute();
List<Question> questions = excecuted.body();
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
callerQuestions.addAll(questions);
callerActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
questionsAdapter.notifyDataSetChanged();
}
});
//Get TotalQuestionCount if not yet
if (((StatefulApplication) callerActivity.getApplication()).getQuestionCount() == -1){
Call<Integer> call2 = service.getQuestionsSize();
try {
((StatefulApplication) callerActivity.getApplication()).setQuestionCount(call2.execute().body());
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return 1;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//TODO: show loader
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
//TODO: hide loader
}
}
如您所见,在 运行 FastStoreModelTransaction
之后一切似乎都很好,没有任何问题发生。没有错误。
初始化作业在启动画面中完成 activity 如下所示:
private void initializeEveryRun() {
//Initializing DBFlow
//DBFlow needs an instance of Context in order to use it for a few features such as reading from assets, content observing, and generating ContentProvider.
//Initialize in your Application subclass. You can also initialize it from other Context but we always grab the Application Context (this is done only once).
FlowManager.init(new FlowConfig.Builder(getApplicationContext()).build());
}
知道什么会导致这个问题或尝试任何解决方案吗?
TG.
我找到了答案!!!
如您在模型中所见,Id
是从服务器检索到的对象的标识符,LocalId
是存储在本地的自动递增标识符。这就是问题所在。我已经使用 Id
字段作为主键,并为服务器端标识符添加了一个名为 OnlineId
的字段,现在一切正常。
这是一个错误还是我用错了?
TG.
这不是执行交易,只是交易创建。
如你所见,这个测试DBFlow - FastModelTest.kt。
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
您必须像这样执行您的交易:
FlowManager.getDatabase(QuestionDatabase.class).executeTransaction(<<YourTransaction>>);
否则,如果您已经有一个 DatabaseWrapper 实例,您可以 <<YourTransaction>>.excute(<<YourDatabaseWrapper>>);
。
我有这样一个数据库:
@Database(name = QuestionDatabase.NAME, version = QuestionDatabase.VERSION)
public class QuestionDatabase {
public static final String NAME = "QuestionDatabase"; // we will add the .db extension
public static final int VERSION = 1;
}
和这样的 table:
@Table(database = QuestionDatabase.class)
public class Question extends BaseModel {
@PrimaryKey
public int localID;
@Column
public int Id;
@Column
public String Answer;
@Column
public String ImageURL;
@Column
public boolean IsFavorite;
@Column
public boolean IsSolved;
}
以及从服务器检索数据的异步任务:
public class QuestionRetriever extends AsyncTask<Integer, Void, Integer> {
private Activity callerActivity;
private QuestionAdapter questionsAdapter;
private List<Question> callerQuestions;
private Integer pageSize = 10;
public QuestionRetriever(Activity callerActivity, QuestionAdapter questionsAdapter, List<Question> questions){
this.callerActivity = callerActivity;
this.questionsAdapter = questionsAdapter;
this.callerQuestions = questions;
}
@Override
protected Integer doInBackground(Integer... pageNumbers) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.33:313/")
.addConverterFactory(GsonConverterFactory.create())
.build();
QuestionWebService service = retrofit.create(QuestionWebService.class);
Call<List<Question>> call = service.getQuestionsPaged(pageNumbers[0].toString(), pageSize.toString());
try {
Response<List<Question>> excecuted = call.execute();
List<Question> questions = excecuted.body();
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
callerQuestions.addAll(questions);
callerActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
questionsAdapter.notifyDataSetChanged();
}
});
//Get TotalQuestionCount if not yet
if (((StatefulApplication) callerActivity.getApplication()).getQuestionCount() == -1){
Call<Integer> call2 = service.getQuestionsSize();
try {
((StatefulApplication) callerActivity.getApplication()).setQuestionCount(call2.execute().body());
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return 1;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//TODO: show loader
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
//TODO: hide loader
}
}
如您所见,在 运行 FastStoreModelTransaction
之后一切似乎都很好,没有任何问题发生。没有错误。
初始化作业在启动画面中完成 activity 如下所示:
private void initializeEveryRun() {
//Initializing DBFlow
//DBFlow needs an instance of Context in order to use it for a few features such as reading from assets, content observing, and generating ContentProvider.
//Initialize in your Application subclass. You can also initialize it from other Context but we always grab the Application Context (this is done only once).
FlowManager.init(new FlowConfig.Builder(getApplicationContext()).build());
}
知道什么会导致这个问题或尝试任何解决方案吗? TG.
我找到了答案!!!
如您在模型中所见,Id
是从服务器检索到的对象的标识符,LocalId
是存储在本地的自动递增标识符。这就是问题所在。我已经使用 Id
字段作为主键,并为服务器端标识符添加了一个名为 OnlineId
的字段,现在一切正常。
这是一个错误还是我用错了?
TG.
这不是执行交易,只是交易创建。
如你所见,这个测试DBFlow - FastModelTest.kt。
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
您必须像这样执行您的交易:
FlowManager.getDatabase(QuestionDatabase.class).executeTransaction(<<YourTransaction>>);
否则,如果您已经有一个 DatabaseWrapper 实例,您可以 <<YourTransaction>>.excute(<<YourDatabaseWrapper>>);
。