1

iOS will purge assets after being downloaded as soon as it needs to free up some space.

Changing the preservation priorities of the assets will not prevent the system from purging them as stated in the "Setting Preservation Priority" section here.

My relevant code to download the On-demand resources is the following:

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()
    }
}

After downloading the On-Demand resources, they can be accessed from the main bundle.

Is there anyway to make audios persist, and hence prevent the system from purging them?

Malloc
  • 15,434
  • 34
  • 105
  • 192
  • This question will probably help you ? Duplicate ?https://stackoverflow.com/questions/33093090/can-ios-9-on-demand-resources-be-kept-permanently –  Apr 27 '18 at 13:30
  • 1
    Possible duplicate of [Can iOS 9 on-demand resources be kept permanently?](https://stackoverflow.com/questions/33093090/can-ios-9-on-demand-resources-be-kept-permanently) – underscore_d Apr 27 '18 at 13:33
  • Did you solve this problem? – RJB Apr 07 '19 at 09:12
  • @RJB Yeap, I posted my answer below. – Malloc Apr 07 '19 at 13:15

1 Answers1

3

In response to @RJB comment above, I will answer my question :)

As soon as the On-demand resources are downloaded, you need to save them in hard disk (The documents directory for example) in order to persist them. Otherwise, iOS will retain the right to purge them as soon as it needs more free space.

Something like the following:

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()
     }
}
Malloc
  • 15,434
  • 34
  • 105
  • 192
  • This saved me. I've one question. After saving the items to the device documents folder, do you need to remove them from the package where they downloaded to? – RJB Apr 11 '19 at 18:18
  • No, they will be removed automatically after a certain amount of time (iOS will purge them when needed). This is the reason why you have to instantly save them in persistent place right after download. Hope I answered your question :) – Malloc Apr 12 '19 at 09:33