0

I am new in Xcode and Swift. Currently working on a script dealing Apple's ODR: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/On_Demand_Resources_Guide/Managing.html#//apple_ref/doc/uid/TP40015083-CH4-SW1

I am using NSBundleResourceRequest as

assetsPack = NSBundleResourceRequest(tags: [tag])

and than download a specific resource from my Assets.xcassets by a given tag with: conditionallyBeginAccessingResources instance method.

The main code snippet I have is:

var assetsPack: NSBundleResourceRequest?

 if let req = assetsPack{
    req.assetsPack()
  }
  
  assetsPack = NSBundleResourceRequest(tags: [tag])
    
   
  guard let req = assetsPack else {
    return
  }
  

  req.conditionallyBeginAccessingResources{available in
    if available{
        
        print("available")
        print(available)
      
        self.anotherFunction(tag)
    } else {
      req.beginAccessingResources{error in
        guard error == nil else{
          return
        }
        self.anotherFunction(tag)
      }
    }

What I need is to return the path of the ODR resource here or pass it to another function. I need to be able and use this path to copy my file to another place, or access to it once its downloaded with another external plugin.

I've been trying some method like:

    let path = req.bundle.url(forResource: "myFile", withExtension: "data")
    print(path)

Considering that i have a myFile of type data in my Assets.xcassets.

But it returns nil.

I've tried also:

let stringPath = Bundle.main.path(forResource: "myFile", ofType: "data")

let urlPath = Bundle.main.url(forResource: "myFile", withExtension: "data")

inoxious
  • 313
  • 2
  • 6
  • 17

1 Answers1

0

It's is possible that you called endAccessingResources before you accessed the resource, or your request is released/nilified too early, which caused the system to deallocate the files.

An irrelevant side note: ODR tags don't seem to support whitespaces in them. If one adds a tag with whitespace, the request will execute without actually downloading the files. The behavior would be conditionallyBeginAccessingResources constantly giving isResourceAvailable = false.

yo1995
  • 471
  • 3
  • 7