Post not yet marked as solved
Hello, I have an architectural question. Imagine this sample
https://developer.apple.com/documentation/swift/asyncstream
slightly modified so it doesn't use a static var
extension QuakeMonitor {
var quakes: AsyncStream<Quake> {
AsyncStream { continuation in
let monitor = QuakeMonitor()
monitor.quakeHandler = { quake in
continuation.yield(quake)
}
continuation.onTermination = { @Sendable _ in
monitor.stopMonitoring()
}
monitor.startMonitoring()
}
}
}
Suppose multiple receivers are interested in getting updates via the quakes stream, how would one architect such solution? E.g. let's say we have two views which exist at the same time such as below. Is there a way both can get updates simultaneously?
Alternatively, is it possible to "hand off" a stream from one object to another?
class MyFirstView: UIView {
private var quakeMonitor: QuakeMonitor
init(quakeMonitor: QuakeMonitor) {
self.quakeMonitor = quakeMonitor
}
func readQuakeData() {
for await quake in quakeMonitor.quakes {
print ("Quakes in first view: \(quake.date)")
}
}
}
class MySecondView: UIView {
private var quakeMonitor: QuakeMonitor
init(quakeMonitor: QuakeMonitor) {
self.quakeMonitor = quakeMonitor
}
func readQuakeData() {
for await quake in quakeMonitor.quakes {
print ("Quakes in second view: \(quake.date)")
}
}
}
Post not yet marked as solved
Hello all,
I am using a AVAggregateAssetDownloadTask to download a stream. In the session's init, the delegateQueue is set to main.
I create and resume session tasks from a separate thread:
func doSomething async {
Task {
//Task setup here
mySessionTask.resume()
}
}
Now, in the delegate, I have:
public func urlSession(
_ session: URLSession,
aggregateAssetDownloadTask: AVAggregateAssetDownloadTask,
willDownloadTo location: URL
) {
print("Location: \(location.path)")
do {
let data = (try location.bookmarkData())
print("Great! We generated bookmark data with length \(data.count)")
} catch {
print("Ooops, an error occurred: \(error)")
}
The problem is, when I run the code I get an error:
Error Domain=NSCocoaErrorDomain Code=260 "The file couldn’t be opened because it doesn’t exist."
I am not sure why this is the case. My code is basically exactly the same as a sample from Apple: https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/using_avfoundation_to_play_and_persist_http_live_streams . I can reproduce my error here if I generate the task from a different thread
This leads me to believe, there is a problem (probably with security scoping, but doesn't make sense to me) when trying to access a URL from a different thread in order to generate a bookmark. Has anyone experience this? Any ideas how it can be fixed?
Thanks in advance.
Post not yet marked as solved
Hi all,
I have seen this discussed a number of times, but I'm afraid I haven't found a solution which works for me.
Gven an AVAggregateAssetDownloadTask, when the user force quits the application, is there a way on launch to resume from where the app left off?
I have tried a number of things:
I have tried to point the application to the locally stored HLS download (seems illogical, but the internet suggested it.
I have implemented
_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?
)
I can see the cancelled task, but there is no resume data associated with it.
Any help would be appreciated.
Post not yet marked as solved
Hi all,
I would like to unit test a piece of code which has logic related to the operation of an AVAggregateAssetDownloadTask
I cannot separate concerns more than I have, so can't really go around the problem. I know I can use URLProtocol to mock data tasks, but it seems AVAssetDownloadURLSession is a bit different.
So far I have tried:
To create an AVURLAsset by pointing it to a stream in my test bundle - no joy (I downloaded the streams from here: https://developer.apple.com/streaming/fps/)
I can't quite figure out how to load the stream via URLProxy in a way that won't result in an error being returned by the task.
Any help would be greatly appreciated!