-
Notifications
You must be signed in to change notification settings - Fork 12
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
[#982] Background syncing #985
Merged
LukasKorba
merged 1 commit into
Electric-Coin-Company:main
from
LukasKorba:982-Background-syncing
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Background Syncing | ||
|
||
## Sources | ||
We encourange you to watch WWDC videos: | ||
- [Advances in App Background Execution]: https://developer.apple.com/videos/play/wwdc2019/707 | ||
- [Background execution demystified]: https://developer.apple.com/videos/play/wwdc2020/10063 | ||
|
||
## Implementation details | ||
There are 4 different APIs and types of background tasks. Each one is specific and can be used for different scenarios. Synchronization of the blockchain data is time and memory consuming operation. Therefore the `BGProcessingTask` has been used. This type of task is designed to run for a longer time when certain conditions are met (watch Background execution demystified). | ||
|
||
### Steps to make it work | ||
1. Add a capability of `background modes` in the settings of the xcode project. | ||
2. Turn the `Background Processing` mode on in the new capability. | ||
3. Add `Permitted background task scheduler identifiers` to the info.plist. | ||
4. Create the ID for the background task in the newly created array. | ||
5. Register the BGTask in `application.didFinishLaunchingWithOptions` | ||
```Swift | ||
BGTaskScheduler.shared.register( | ||
forTaskWithIdentifier: <ID>, | ||
using: DispatchQueue.main | ||
) { task in | ||
// see the next steps | ||
} | ||
``` | ||
Note: The queue is an optional and most tutorials leave the parameter `nil` but Zashi requires main thread processing due to UI layer - therefore we pass `DispatchQueue.main`. | ||
6. Call a method that schedules the task execution. | ||
7. Start the synchronizer. | ||
8. Set the expiration closure and stop the synchronizer inside it | ||
```swift | ||
task.expirationHandler = { | ||
synchronizer.stop() | ||
} | ||
``` | ||
9. The body of the registered task summarized: | ||
```swift | ||
BGTaskScheduler.shared.register(...) { task in | ||
scheduleTask() // 6 | ||
|
||
synchronizer.start() // 7 | ||
|
||
task.expirationHandler = { | ||
synchronizer.stop() // 8 | ||
} | ||
} | ||
``` | ||
10. Call `scheduleTask()` when app goes to the background so there is the initial scheduling done, the next one will be handled by the closure of the registered task. The method usually consists of: | ||
```Swift | ||
let request = BGProcessingTaskRequest(identifier: <ID>) | ||
|
||
request.earliestBeginDate = <scheduledTime> | ||
request.requiresExternalPower = true // optional, we require the iPhone to be connected to the power | ||
request.requiresNetworkConnectivity = true // required | ||
|
||
do { | ||
try BGTaskScheduler.shared.submit(request) | ||
} catch { // handle error } | ||
``` | ||
11. Last step is to call `.setTaskCompleted(success: <bool>)` on the BGTask when the work is done. This is required by the system no matter what. We call it with `true` when the synchronizer finishes the work (up-to-date state) and with `false` for other or failed reasons (stopped state, error state, etc.). | ||
|
||
You can see specific details of the Zashi implementation in: | ||
- Xcode project settings, steps 1-4. | ||
- AppDelegate.swift file, steps 5-9. | ||
- SecantApp.swift file, step 10. | ||
- RootInitialization.swift, step 11. | ||
|
||
## Gotchas | ||
- The `requiresNetworkConnectivity` flag doesn't specify or deal with the type of connectivity. It simply allows scheduling when the iPhone is connected to the internet. We deal with it when the task is triggered. The custom check wheather the wifi is or is not connected preceeds the start of the synchronizer. | ||
- When the app is killed by a user in the app switcher, the scheduled BGTask is deleted. So the BGTask is triggered at the scheduled time only when the app is suspended or killed by the system. Explicit termination of the app by a user leads to termination of any background processing. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
modules/Sources/Models/AppDelegate.swift → ...es/Sources/Models/AppDelegateAction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
// | ||
// AppDelegate.swift | ||
// AppDelegateAction.swift | ||
// secant-testnet | ||
// | ||
// Created by Lukáš Korba on 27.03.2022. | ||
// | ||
|
||
import Foundation | ||
import BackgroundTasks | ||
|
||
public enum AppDelegateAction: Equatable { | ||
case didFinishLaunching | ||
case didEnterBackground | ||
case willEnterForeground | ||
case backgroundTask(BGProcessingTask) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend using reset numbering here as it's more future-proof, i.e.
See https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#lists