我从API调用中获取多个对象,并且我试图将它们显示到ListView中。
代码如下
public class ExerciseActivity extends AppCompatActivity {
private CompositeDisposable disposables = new CompositeDisposable();
private ExerciseClient exerciseClient;
private List<Exercise> exerciseList;
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
exerciseList = new ArrayList<>();
this.realm = Realm.getDefaultInstance();
exerciseClient = new ExerciseClient(this);
populateExerciseList();
}
private void setupExerciseList() {
ListView lvItems = findViewById(R.id.lvItems);
ArrayAdapter<Exercise> adapter = new ArrayAdapter<>(this, R.layout.list_view, exerciseList);
lvItems.setAdapter(adapter);
}
void populateExerciseList() {
disposables.add(exerciseClient.getExercises()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
this::getExercisesSuccess,
this::getExercisesError
)
);
}
private void getExercisesError(Throwable throwable) {
exerciseList = realm.where(Exercise.class).findAll();
setupExerciseList();
}
private void getExercisesSuccess(List<Exercise> exercises) {
this.realm.executeTransaction(realm -> realm.where(Exercise.class).findAll().deleteAllFromRealm());
for (int i = 0; i < exercises.size(); i++) {
Exercise foundExercise = exercises.get(i);
this.exerciseList.add(foundExercise);
this.realm.executeTransactionAsync(realm -> realm.copyToRealmOrUpdate(foundExercise));
}
setupExerciseList();
}
列表只会加载0到3-4个元素,如果我评论 this.realm.executeTransactionAsync(realm -> realm.copyToRealmOrUpdate(foundExercise));
行,列表会按预期加载。
请记住,我必须从API中收集1000个对象。
我的猜测是this.realm.executeTransactionAsync(realm -> realm.copyToRealmOrUpdate(foundExercise));
需要太长的时间来计算,因此在应用程序启动时我只能得到&lt; 5个条目。
有没有可行的解决方法?
编辑:另外,我已经将数据库中的一些条目删除到只有~50,现在它甚至可以使用该行,所以问题绝对是大块数据。
答案 0 :(得分:0)
怎么样
public class ExerciseActivity extends AppCompatActivity {
private CompositeDisposable disposables = new CompositeDisposable();
private ExerciseClient exerciseClient;
private Realm realm;
private RealmResults<Exercise> results;
private RealmBaseAdapter<Exercise> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
exerciseList = new ArrayList<>();
this.realm = Realm.getDefaultInstance();
this.results = realm.where(Exercise.class).findAllAsync();
this.adapter = new RealmBaseAdapter<Exercise>(results);
exerciseClient = new ExerciseClient(this);
populateExerciseList();
ListView lvItems = findViewById(R.id.lvItems);
lvItems.setAdapter(adapter);
}
void populateExerciseList() {
disposables.add(exerciseClient.getExercises()
.subscribeOn(Schedulers.io())
.subscribe(
this::getExercisesSuccess,
this::getExercisesError
)
);
}
private void getExercisesError(Throwable throwable) {
runOnUiThread(() -> {
// d'oh
});
}
private void getExercisesSuccess(List<Exercise> exercises) {
try(Realm r = Realm.getDefaultInstance()) {
r.executeTransaction(realm -> {
realm.where(Exercise.class).findAll()
.deleteAllFromRealm();
for(Exercise exercise: exercises) {
realm.insertOrUpdate(exercise);
}
});
}
}
}