I am using WKURLSchemeHandler
to serve local files that cannot be read directly by the WkWebView
. While it works fine for small files like videos, it totally freezes the application when it has to provide big files like videos.
Right now, I am reading the full file in a single step which is inappropriate for important files.
data = try Data(contentsOf: fileUrl)
How should I do in order to avoid this? Is there a way to "stream" the content of the file?
didReceive(Data)
is special and can be called multiple times with partial data, so chunked reading/streaming is possible. It's not obvious from the docs, but they said so at their presentation uf the scheme handler feature (see 32:30): https://developer.apple.com/videos/play/wwdc2017/220/Furthermore,
WKURLSchemeTask
's functions can be called on a background thread, no need to do it on the main thread (and potentially blocking the UI).