Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fetch synchronously in the background if no foreground service is allowed #3501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Hocuri
Copy link
Collaborator

@Hocuri Hocuri commented Dec 17, 2024

Fix #3500.

// we need to handle the message within 20s, and the time window may be even shorter than 20s,
// so, use 10s to be safe.
fetchingSynchronously = true;
if (ApplicationContext.dcAccounts.backgroundFetch(10)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also make it 15 instead of 10 seconds if we want to.

Comment on lines +49 to +58
// The background fetch was successful, but we need to wait until all events were processed.
// After all events were processed, we will get DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE,
// and stop() will be called.
while (fetchingSynchronously) {
try {
// The `wait()` needs to be enclosed in a while loop because there may be
// "spurious wake-ups", i.e. `wait()` may return even though `notifyAll()` wasn't called.
STOP_NOTIFIER.wait();
} catch (InterruptedException ex) { }
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to simplify the logic here, we could also simply call Thread.sleep(1000) and assume that the UI will be done with showing a notification within 1s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the logic is too complicated, and it is well documented, better than some fixed sleeping that might be too much in some cases and too little in another

@Hocuri Hocuri changed the title [WIP] fix: Fetch synchronously in the background if no foreground service is allowed fix: Fetch synchronously in the background if no foreground service is allowed Dec 17, 2024
@Hocuri Hocuri requested review from r10s and adbenitez December 17, 2024 15:55
Copy link

To test the changes in this pull request, install this apk:
📦 app-preview.apk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when receiving a message from FCM but battery usage is restricted
2 participants