iOS 按需资源下载正常 - 但在我需要使用它们时,应用程序无法在主包中找到它们

iOS On Demand resources download fine - but they cannot be found in the main bundle by the app at the point I need to use them

我已经浏览了 iOS 中与随需应变资产相关的文档和所有关于 SO 的问题,但找不到答案,因此希望您能提供帮助。我有一个能够正常访问文件的应用程序(特别是 group/folder 中名称为 "GameVideos" 的视频 mp4 文件)。我用来执行此操作的代码是:

guard let url = Bundle.main.url(forResource: videoName, withExtension: "mp4") else {
   print("\(videoName) not found")
   return
}

let item = AVPlayerItem(url: url)
self.player1 = AVPlayer(playerItem: item)
self.videoNode1 = SKVideoNode(avPlayer: self.player1)

现在我已经切换到制作视频 'On demand' 我看到 'not found' 消息被打印出来了。当它没有被列为按需资源时,我不需要引用子目录——没有它它也能正常工作(尽管我尝试添加它只是为了看看)。我也很高兴我没有拼错文件名,因为它也能正常工作。

创建按需资源时 - 我已使用标签进行设置并使用以下代码调用资源(此代码位于 GameViewController.swift 和 "viewDidLoad" 覆盖内- 我正在构建一个 SpriteKit 游戏,所以只有一个视图控制器 - 所有其他逻辑都在 SKScenes 中处理。):

let tags = NSSet(array: [
            "OD2",
            "OD3",
            "OD4",
            "OD5",
            "OD6",
            "OD7",
            "OD8",
        ])

  let resourceRequest = NSBundleResourceRequest(tags: tags as! Set<String>)

  resourceRequest.conditionallyBeginAccessingResources(completionHandler: {(resourcesAvailable: Bool) in
            if !resourcesAvailable  { // if resources not available - start download
                resourceRequest.beginAccessingResources(completionHandler: {(err: Error?) in
                    if let error = err {
                        print("Error getting On Demand Resources: \(error)")
                    }
                })
            }
        })

此代码似乎有效,我可以在 Xcode 上的磁盘监视器中看到按需包已成功下载。

我尝试过的其他事情: 我仔细检查了视频的目标成员,它已正确分配。
我试过用列出的文件夹的子目录调用 Bundle.Main.url(这也不起作用) 我试过重命名文件夹目录(因为它的名称中有 space) 我已经多次尝试清理构建

None 上面的方法有效...我做错了什么??任何猜测或指示将不胜感激。

所以经过大量的试验和错误 - 我将变量 'resourceRequest' 放在全局范围内并且它有效 - XCode 磁盘上的消息从 "downloaded" 变为 "in use"。正如您在我的代码中看到的,我之前在 GameViewController 中定义了 'resourceRequest'。我曾认为这适用于 SpriteKit 游戏,因为 GameViewController 加载然后它控制所有场景(所以我认为它没有超出范围)。希望比我聪明的人会说出为什么这行得通 - 但暂时 - 我很高兴我可以继续并启动我的应用程序(Apple 关于这个东西的文档很糟糕)