I'm getting multi objects from an API call and I'm trying to display the them into a ListView.
Code is as follows
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();
}
The list would only load between 0 and 3-4 elements, if I comment the this.realm.executeTransactionAsync(realm -> realm.copyToRealmOrUpdate(foundExercise));
line, the list loads as expected.
Keep in mind I have 1000 objects I have to gather from the API.
My guess is that the this.realm.executeTransactionAsync(realm -> realm.copyToRealmOrUpdate(foundExercise));
takes too long to compute thus by the time the app starts I only get < 5 entries.
Is there any possible workarounds?
Edit: Also, I've deleted some entries from the database to only ~50 and it seems to work now even with that line present, so the problem is definitely the big chunk of data.