swift 退出应用程序后,按需资源是否真的持续存在?

xqkwcwgp  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(105)

我已经创建了NSBundleResourceRequest对象作为类的示例成员。如果我退出应用程序的请求对象是deallocatted.根据apple文档,如果请求对象被释放,则下载的资产的控制由系统负责,并且数据可能被永久删除。我的疑问是,如果一个游戏是使用ODR为每个级别的 meta数据,我们不下载资源,而每次重新打开应用程序。ODR下载资产后,是否必须手动保存?

class ViewController: UIViewController {
    var request = NSBundleResourceRequest(tags: Set(arrayLiteral: "prefetch"))
    
    lazy var imageView1:UIImageView = {
        let imageView  = UIImageView(image: UIImage(named: "car"))
        imageView.layer.borderColor = UIColor.red.cgColor
        imageView.layer.borderWidth = 2
        return imageView
    }()
    let label = UILabel()
    lazy var  imageView2:UIImageView = {
        let imageView  = UIImageView(image:UIImage(named: "hd1"))
        imageView.layer.borderColor = UIColor.red.cgColor
        imageView.layer.borderWidth = 2
        return imageView
    }()
    
    lazy var imageView3:UIImageView = {
        let imageView  = UIImageView(image:UIImage(named: "hd2"))
        imageView.layer.borderColor = UIColor.red.cgColor
        imageView.layer.borderWidth = 2
        return imageView
    }()
    
    let button:UIButton = {
//        let config = UIButton.Configuration.bordered()
        let button = UIButton()
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .green
        button.setTitle("Load HD image", for: .normal)
        return button
    }()
    
    override func loadView() {
        super.loadView()
        loadImage()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureImageViews()
        fetchImage()
    }

    func configureImageViews(){
        view.addSubview(imageView1)
        view.addSubview(imageView2)
        view.addSubview(imageView3)
        view.addSubview(button)
        view.addSubview(label)
        
        label.translatesAutoresizingMaskIntoConstraints = false
        imageView1.translatesAutoresizingMaskIntoConstraints = false
        imageView2.translatesAutoresizingMaskIntoConstraints = false
        imageView3.translatesAutoresizingMaskIntoConstraints = false
        button.translatesAutoresizingMaskIntoConstraints = false

        label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
        label.heightAnchor.constraint(equalToConstant: 40).isActive = true
        
        imageView1.topAnchor.constraint(equalTo: label.safeAreaLayoutGuide.bottomAnchor).isActive = true
        imageView1.heightAnchor.constraint(equalToConstant: 200).isActive = true
        imageView1.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        imageView1.widthAnchor.constraint(equalToConstant: 200).isActive = true
        
        imageView2.topAnchor.constraint(equalTo: imageView1.safeAreaLayoutGuide.bottomAnchor,constant: 20).isActive = true
        imageView2.heightAnchor.constraint(equalToConstant: 200).isActive = true
        imageView2.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        imageView2.widthAnchor.constraint(equalToConstant: 200).isActive = true
        
        imageView3.topAnchor.constraint(equalTo: imageView2.safeAreaLayoutGuide.bottomAnchor,constant: 20).isActive = true
        imageView3.heightAnchor.constraint(equalToConstant: 200).isActive = true
        imageView3.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        imageView3.widthAnchor.constraint(equalToConstant: 200).isActive = true
        
        button.topAnchor.constraint(equalTo: imageView3.safeAreaLayoutGuide.bottomAnchor,constant: 20).isActive = true
        button.heightAnchor.constraint(equalToConstant: 40).isActive = true
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.widthAnchor.constraint(equalToConstant: 200).isActive = true
        
        button.addTarget(self, action: #selector(buttonDidTapped), for: .touchUpInside)
    }
    
    func fetchImage(){
        request.endAccessingResources()
        self.request.conditionallyBeginAccessingResources(completionHandler: {isImageAvailable in
            if isImageAvailable == true{
                //image already available
                print("--> Local data available")
                self.loadImage()
                DispatchQueue.main.async {
                    self.label.text = "Local data available"
                }
            }else{
                //download from app store
                self.request.beginAccessingResources(completionHandler: {error in
                    if let error{
                        print("--> something went wrong")
                        self.label.text = "something went wrong"
                        return
                    }
                    DispatchQueue.main.async {
                        self.label.text = "fetching remote"
                    }
                    self.loadImage()
                })
            }
            
        })
    }

    
    func loadImage(){
        DispatchQueue.main.async {
            self.imageView1.image = UIImage(named: "car")
            self.imageView2.image = UIImage(named: "hd1")
            self.imageView3.image = UIImage(named: "hd2")
        }
    }
    
   @objc func buttonDidTapped(){
        request = NSBundleResourceRequest(tags: Set(arrayLiteral: "hd1"))
       fetchImage()
    }
}

我想,我们如何才能确保网上解决数据不会再次下载?

gfttwv5a

gfttwv5a1#

对特定ODR资源的使用必须包含对beginAccessingResourcesendAccessingResources的调用,在这两个调用之间保留NSBundleResourceRequest。这就是你知道的和你需要知道的。
如果您的应用在调用endAccessingResources之前终止,那么在重新启动时,您显然没有NSBundleResourceRequest示例;但这并不重要,因为您只需遵循有关如何访问资源的规则。在启动时,立即创建NSBundleResourceRequest并调用beginAccessingResources;您的资源可能仍然存在,因此您将立即获得访问权限,并且您对NSBundleResourceRequest的保留将使资源继续存在,直到您最终通过说endAccessingResources来释放它们。

相关问题