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

Add minimum time interval and default value to operation polling #151

Merged
merged 7 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions WultraMobileTokenSDK/Operations/Service/WMTOperationsImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
private var isPollingPaused: Bool { return pollingTimer?.isValid == false }
private let pollingLock = WMTLock()
private var notificationObservers = [NSObjectProtocol]()
private let minimumTimePollingInterval = TimeInterval(5)
Hopsaheysa marked this conversation as resolved.
Show resolved Hide resolved

/// Operation register holds operations in order
private lazy var operationsRegister = OperationsRegister { [weak self] ops, added, removed in
Expand Down Expand Up @@ -364,8 +365,17 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations, WMTService {
refreshOperations()
}

D.print("Operations polling started with \(interval) seconds interval")
pollingTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
// Set the minimum TimeInterval
var adjustedInterval: TimeInterval
if interval < minimumTimePollingInterval {
D.warning("Operations polling interval: \(interval), must not be set below \(minimumTimePollingInterval) to prevent server overload.")
adjustedInterval = minimumTimePollingInterval
} else {
adjustedInterval = interval
}

D.print("Operations polling started with \(adjustedInterval) seconds interval")
pollingTimer = Timer.scheduledTimer(withTimeInterval: adjustedInterval, repeats: true) { [weak self] _ in
self?.refreshOperations()
}
}
Expand Down
12 changes: 12 additions & 0 deletions WultraMobileTokenSDK/Operations/WMTOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ public extension WMTOperations {
func authorize(qrOperation: WMTQROperation, authentication: PowerAuthAuthentication, completion: @escaping (Result<String, WMTError>) -> Void) -> Operation {
return authorize(qrOperation: qrOperation, uriId: "/operation/authorize/offline", authentication: authentication, completion: completion)
}

/// Starts the operations polling.
///
/// The default value for `interval` is 7 seconds
Hopsaheysa marked this conversation as resolved.
Show resolved Hide resolved
///
/// If operations are already polling this call is ignored and
/// polling interval won't be changed.
/// - Parameter delayStart: When true, polling starts after
/// the first `interval` time passes
func startPollingOperations(delayStart: Bool) {
Hopsaheysa marked this conversation as resolved.
Show resolved Hide resolved
return startPollingOperations(interval: TimeInterval(7), delayStart: delayStart)
Hopsaheysa marked this conversation as resolved.
Show resolved Hide resolved
}
}

public typealias GetOperationsResult = Result<[WMTUserOperation], WMTError>
Expand Down
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.10.0

- Removed `currentServerTime` property [(#148)](https:/wultra/mtoken-sdk-android/pull/139)
- Implemented default and minimum pollingInterval [(#151)](https:/wultra/mtoken-sdk-ios/pull/151)

## 1.9.0 (Jan 24, 2024)

Expand Down
10 changes: 10 additions & 0 deletions docs/Using-Operations-Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ if (!operationsService.isPollingOperations) {
}
```

### Default and Minimum TimeInterval Enforcement

For convenience, there is a default implementation where you can omit the polling interval and it is automatically set to 7 seconds. If you specify an interval below 5 seconds, it will be automatically adjusted to 5 seconds to prevent server overload.

### Setting up a delegate

To receive the result of the polling, set up a delegate.

<!-- begin box warning -->
Expand Down Expand Up @@ -148,6 +154,10 @@ class MyOperationsManager: WMTOperationsDelegate {
Polling behavior can be adjusted by the `pollingOptions` parameter when [creating an instance](#creating-an-instance) of the service.
<!-- end -->

### Best Practices and Recommendations

For optimal server performance, consider adjusting polling intervals based on your application's requirements. For instance, when push notifications are enabled, it's advisable to double the polling interval to minimize server load.

## Approve an Operation

To approve an operation use `WMTOperations.authorize`. You can simply use it with the following examples:
Expand Down
Loading