iOS正在清除下载的mp3点播资源,如何防止?
iOS is purging On-Demand resources downloaded mp3, how to prevent this?
iOS 将在下载后清除资产,一旦它需要释放一些 space。
更改资产的保存优先级不会阻止系统清除它们,如 "Setting Preservation Priority" 部分 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()
}
}
下载点播资源后,可以从主包访问它们。
有没有办法让音频持久化,从而防止系统清除它们?
针对@RJB 上面的评论,我会回答我的问题:)
点播资源下载完成后,您需要将它们保存在硬盘中(例如文档目录),以便持久化。否则,iOS 将保留在需要更多免费空间时立即清除它们的权利 space。
类似于以下内容:
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()
}
}
iOS 将在下载后清除资产,一旦它需要释放一些 space。
更改资产的保存优先级不会阻止系统清除它们,如 "Setting Preservation Priority" 部分 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()
}
}
下载点播资源后,可以从主包访问它们。
有没有办法让音频持久化,从而防止系统清除它们?
针对@RJB 上面的评论,我会回答我的问题:)
点播资源下载完成后,您需要将它们保存在硬盘中(例如文档目录),以便持久化。否则,iOS 将保留在需要更多免费空间时立即清除它们的权利 space。
类似于以下内容:
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()
}
}