armpatch / TrainingLog

Android app for tracking workouts
3 stars 3 forks source link

Delay when adding sets #30

Closed armpatch closed 4 years ago

armpatch commented 4 years ago

Problem: Sometimes when the add set button is clicked, no set is added to the UI. It seems that the set is added to the database, because the next time the add set button is clicked, both sets will show up.

I suspect the logic to update the UI from the database is executing before the database has been updated, so no sets are retrieved the first time around.

ir31k0 commented 4 years ago

Hey, I believe the database executor is the reason. It's slower than the adapter's code. So if I remove it there is no issue.

WorkoutRepository.java void insert(final ExerciseSet set) { exerciseSetDao.insert(set); }

What is the reason you use an executor when inserting, but not when updating or deleting?

Another way is you can add the set manually via the adapter like:

TrackerSetAdapter @Override protected void onPostExecute(Void aVoid) { sets.add(set); notifyDataSetChanged(); }

And my last idea is an event manager that triggers an event when inserted and the ExerciseTrackerActivity listens to this event and passes it to the adapter.

Do you have a better suggestion?

armpatch commented 4 years ago

void insert(final ExerciseSet set) { exerciseSetDao.insert(set); }

I removed the database executor like you said, and it works great.

What is the reason you use an executor when inserting, but not when updating or deleting?

I mistakenly thought that it was necessary to use an executor when doing an insert statement. I realize now, this would only be necessary if the insert statement was not already being called asyncronously using AsyncTask. All the WorkoutRepository methods are already being called separate from the UI thread, so no need to use an executor any more.

I will be pushing an update momentarily.

armpatch commented 4 years ago

Fixed in this commit: e072bcd6f248dcf88125d60e2b05f5eea11f924f