tylerbwong / stack

An Android app for browsing Stack Overflow and other Stack Exchange sites.
https://stack.tylerbwong.me
GNU General Public License v3.0
522 stars 36 forks source link

Fix Compounding WorkRequest Bug #104

Closed tylerbwong closed 3 years ago

tylerbwong commented 3 years ago

Description

Fixes #93.

Background work was not being enqueued as unique work. This meant that every time a user opened the app, a new set of WorkRequests would get enqueued, but existing work requests would not be canceled. This was especially noticeable with BookmarksWorker, which is a PeriodicWorkRequest that ran every 8 hours. Every time a user opened the app, a new request would be scheduled to be run every 8 hours from that point.

The side-effects of this bug would be that work requests would compound and eventually attempt to make a large number of requests to the Stack Exchange API at once. This would result in 503s being returned and the "Network error" message to appear when making any API request.

This PR introduces a fix going forward that will check WorkManager for existing work based on new identifiers on each WorkRequest. If no currently enqueued work is found for any new identifier, cancel all WorkRequests since we can assume the user has a ton of non-unique work enqueued. Then schedule the unique work as normal (based on the ExistingWorkPolicy).

This overall disables offline bookmarks for now until a more sustainable solution is developed.

Checklist