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
Improve BackgroundObserver #781
Improve BackgroundObserver #781
Conversation
Following best-practice recommendations, BackgroundObserver now registers a background task for every attached Procedure and ends the task once the Procedure has finished (instead of waiting until background events occur and trying to register / end background tasks then). BackgroundObserver now supports an optional CancellationBehavior for automatically cancelling the attached Procedure when certain background-related events occur. An internal background manager now observes NotificationCenter background events once, instead of the previous observation inside every BackgroundObserver instance. Numerous bug fixes, including: - Adding a single BackgroundObserver instance to multiple Procedures now works. - BackgroundObserver properly handles the case where running in the background is not possible. - BackgroundObserver no longer attempts to access main-thread-only properties of UIApplication on other threads. And exhaustive testing improvements, including fixes to the TestableUIApplication and many additional test cases.
@swiftlyfalling shall we hold v4.3 for this? |
@danthorpe: Let's target this for v4.4. (I'd like to run tests on some additional edge cases on real devices, and it'll be a little bit.) |
I'm debating about the As-is, Hence, I think the actual effect of Thoughts? |
While this would be really nice functionality, I think I have to agree. I don't know that it would be dangerous, but it would certainly be misleading. On a related note, in the |
@jshier: Re: The current code registers a background task when the application enters the background - so it ought to register a new task if the app lifecycle is: This PR registers a background task immediately (as Apple recommends), but doesn't do so a second time (after one cycle of being backgrounded -> active). The thinking was that the default would actually change from However, I think modifying this PR to request a new background task if the prior one expired (and the app becomes active again) makes sense. It would more closely match the prior behavior. (Even if it only comes into play when |
…to feature/OPR-781_improve_backgroundobserver
Okay, so following up on this, we've got some outstanding changes to address, I think.
It seems like the 1st point should be done before the PR is merged, but perhaps the 2nd point can come later, as it's "just" additionally handling an edge case. I'm also looking at the CodeBeat static analysis of this change set - as it introduces quite a few issues. Generally, deeper type nesting (maybe the registry should not be a nested class?) and increased Assignment Branch Condition - which could be minimised with some functions. @swiftlyfalling I can make these changes, and get this merged. |
Improvements and New Features
Following best-practice recommendations,
BackgroundObserver
now registers a background task for every attachedProcedure
(instead of waiting until background events occur and trying to register background tasks then) and ends the registered background task once theProcedure
has finished or if background execution time expires.BackgroundObserver
now supports an optionalCancellationBehavior
for automatically cancelling the attachedProcedure
when certain background-related events occur. Supported values are:.never
: do not cancel the attached Procedure (the default).whenBackgroundExecutionTimeExpires
: cancel the attached Procedure when the associated background task's expiration handler is called.whenAppIsBackgrounded
: cancel the attached Procedure when the app is backgrounded (or if it is already in the background)An internal background manager now observes
NotificationCenter
background events once, instead of the previous observation inside everyBackgroundObserver
instance, reducing overhead and improving performance.Bug Fixes
And exhaustive testing improvements, including fixes to the TestableUIApplication and many additional test cases:
UIBackgroundTaskInvalid
for beginBackgroundTask, because it relied on calling UIApplication.shared and background execution is not supported in the test runner (so attempting to actually start background tasks fails). Now, the TestableUIApplication handles mocking up the required APIs and events itself.