I want to run some code in withExclusiveControl and only when the downloads count is 0
Approach 1
manager.fetchCurrentDownloads { downloads, e in
manager.withExclusiveControl { a, e in
// Here we are in ExclusiveControl but checking for downloads.count == 0 might not give accurate value
}
}
Approach 2
manager.withExclusiveControl { a, e in
manager.fetchCurrentDownloads { downloads, e in
// Here we can check for downloads.count == 0, but not actually in ExclusiveControl
}
}
Can you please let me know how to achieve this?
Here you go, added in iOS 16.4. https://developer.apple.com/documentation/backgroundassets/badownloadmanager/4143373-fetchcurrentdownloads
If you need to support older releases, you'll want to use a DispatchSemaphore
to make the async version synchronous.
I'd highly discourage that approach though as the kernel will be unable to propagate your thread's QoS to the system daemon. The new synchronous API does not have this problem.