https://github.com/timonus/tjbackgroundtask
Convenient wrapper around UIApplication background tasks
https://github.com/timonus/tjbackgroundtask
background-refresh objective-c uiapplication uikit
Last synced: 7 months ago
JSON representation
Convenient wrapper around UIApplication background tasks
- Host: GitHub
- URL: https://github.com/timonus/tjbackgroundtask
- Owner: timonus
- License: bsd-3-clause
- Created: 2021-03-01T20:39:44.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-14T03:31:28.000Z (about 2 years ago)
- Last Synced: 2025-02-18T01:38:36.418Z (over 1 year ago)
- Topics: background-refresh, objective-c, uiapplication, uikit
- Language: Objective-C
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TJBackgroundTask
This project is a wrapper around [`UIApplication`'s background task APIs](https://developer.apple.com/documentation/uikit/uiapplication/1623051-beginbackgroundtaskwithname?language=objc) that reduces boiler plate and helps avoid common pitfalls.
## What does this do for me?
This project class does the following handy things
- It automatically ends background tasks that are about to expire.
- It uses object lifecycles instead of background task IDs for managing tasks, which makes it harder to "leak" or mismanage tasks. Tasks are automatically ended when `TJBackgroundTask`s are deallocated.
- It avoids starting background tasks if < 5 seconds of background time remains, which is recommended in [this WWDC talk](https://developer.apple.com/videos/play/wwdc2020/10078/?t=640).
## Usage
```objc
TJBackgroundTask *const task = [[TJBackgroundTask alloc] initWithName:@"my async work"];
dispatch_async(..., ^{
// Do some work
// ...
[task endTask];
});
// or, more simply
TJBackgroundTask *const task = [[TJBackgroundTask alloc] initWithName:@"my sync work"];
// Do some work
// ...
[task endTask];
```
Some notes:
- You can call `-endTask` any number of times, it'll do the right thing if you call it more than once.
- `TJBackgroundTask`'s initializers will return `nil` if the app is in a state it deems isn't eligible for background tasks.
- `TJBackgroundTask` can be used from any thread if you're into that sort of thing.