iOS会在需要释放一些空间后立即清除资产。
更改资产的保存优先级不会阻止系统按照"设置保存优先级"中所述的方式清除它们。第here节。
下载按需资源的相关代码如下:
func requestResourceWith(tag: [String],
onSuccess: @escaping () -> Void,
onFailure: @escaping (NSError) -> Void) {
currentRequest = NSBundleResourceRequest(tags: Set(tag))
guard let request = currentRequest else { return }
request.endAccessingResources()
request.loadingPriority =
NSBundleResourceRequestLoadingPriorityUrgent
request.beginAccessingResources { (error: Error?) in
if let error = error {
onFailure(error as NSError)
return
}
onSuccess()
}
}
下载按需资源后,可以从主捆绑中访问它们。
有没有让音频持续存在,从而阻止系统清除它们?
答案 0 :(得分:1)
针对上述@RJB评论,我将回答我的问题:)
一旦下载了按需资源,您就需要将其保存在硬盘中(例如,documents目录)以便持久保存它们。否则,iOS将保留在需要更多可用空间时立即清除它们的权利。
类似以下内容:
request.beginAccessingResources { (error: Error?) in
if let error = error {
DispatchQueue.main.async {
onFailure(error as NSError)
}
return
}
// Move ODR downloaded assets to Documents folder for persistence
DispatchQueue.main.async {
let path: String! = Bundle.main.path(forResource: "filename", ofType: "mp3")
let sourceURL = URL(fileURLWithPath: path)
let destinationURL = // Build a destination url in the Documents directory or any other persistent Directory of your choice
do {
try FileManager.default.copyItem(at: sourceURL, to: destinationURL)
}catch {
// Handle error accordingly
}
onSuccess()
}
}