ServicePredictionProvider
A class that provices cellular service predictions about upcoming events and anomalies.
Declaration
final class ServicePredictionProviderOverview
To receive predictions about changing cellular conditions, start by initializing and retaining an instance of this class. Next, iterate over the servicePredictions asynchronous sequence with a for-await-in loop.
Each time your loop receives updated ServicePrediction instances, you can inspect their predictedStartTime, predictedInterval, and impact properties to determine if your app needs to take action in advance of a potentially disruptive event.
While processing predictions from the servicePredictions sequence, gracefully handle any errors that it throws. The ServicePredictionError.unsupportedDevice error indicates that WirelessInsights won’t provide any predictions on this device. However, other errors indicate recoverable failures, such as ServicePredictionError.connectionError.
The following example shows a class that creates a Task to receive service predictions and call a private helper method when it receives a sufficiently important prediction.
class PredictionHandler {
private let servicePredictionProvider = ServicePredictionProvider()
private let predictionHandlerTask: Task<Void, Never>
init() {
predictionHandlerTask = Task {
do {
for try await predictions in servicePredictionProvider.servicePredictions {
// Take action if any prediction in the current set
// has a sufficiently high impact and duration,
// with medium or higher confidence.
for prediction in predictions {
if prediction.confidenceScore.prediction == .high &&
prediction.impact >= .medium &&
prediction.predictedInterval > QuantizedInterval.short {
self.prepareForDisruptiveWirelessEvent()
}
}
}
} catch {
handlePredictionsError(error)
}
}
}
private func prepareForDisruptiveWirelessEvent() {
// Prepare for possible degradation of wireless connectivity
// by caching more data now, temporarily increasing
// retry count, or delaying network requests.
}
private func handlePredictionsError(_ error: Error) {
// If error is .connectionError, consider waiting and then retrying
// the loop over servicePredictions.
// For other errors, such as .unsupportedDevice, don't expect the
// ServicePredictionProvider to produce any predictions.
}
}