I’ve an app that listens to silent push notifications from my server whereas being within the background (by way of didReceiveRemoteNotification
) to carry out some community operations utilizing URLSession.knowledge.activity
. I am new to Swift and I am engaged on an current codebase that nests a number of requests when a push is obtained on this vogue:
URLSession.shared.dataTask(with: request) {knowledge, response, error in
//do some others URLSession.shared.dataTask operations
}.resume()
My didReceiveRemoteNotification
appears to be like like this:
func software(_ software: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handlePush(userInfo: userInfo, metric: Metric.ALAPPBGPushFetchRequested)
completionHandler(UIBackgroundFetchResult.noData)
}
The Downside, when the silent push is obtained,completionHandler
is known as earlier than all different URLSessions
are accomplished. Ideally, I must name completionHandler
from inside these URLSession
as soon as these requests are resolved. I can’t simply go the reference of the completionHandler
as a result of there is not a direct hierarchy that permits me to go it down (handlePush
triggers another listeners, and many others).
What could possibly be a great resolution to make use of this completionHandler
from a unique scope aside from the unique one? I thought of storing the completionHandler
in a world variable after which utilizing it however I am getting sudden exceptions generally, doesn’t appears dependable, until I am simply doing poor swift. I additionally would not wish to refactor the chain of calls that occurs after handlePush
as a result of most of them are sensor listeners that ultimately calls these URLSession
.