0

Apple のオンデマンド リソース(ODR)を利用している SwiftUI プロジェクトがあります。

リソースのダウンロードの進行状況を示すプログレス バーを表示したいと思います。

私が試したこと:

次のコードは、ODR リソースを要求NSBundleResourceRequestし、静的プロパティに格納して、他の場所で使用できるようにします。

class OnDemandResources {
    static var request: NSBundleResourceRequest?

    func loadMusic() {
        OnDemandResources.request = NSBundleResourceRequest(tags: Set(["Music"]))
        
        OnDemandResources.request?.conditionallyBeginAccessingResources { resourcesAvailable in
            DispatchQueue.main.async {
                if resourcesAvailable {
                    //Resource is already on the device. No need to download.
                    
                } else {
                    //Resource needs to be downloaded.
                    
                    OnDemandResources.request?.beginAccessingResources { error in
                        DispatchQueue.main.async {
                            if error == nil {
                                //Success
                            }
                        }
                    }
                }
            }
        }
    }
    
}

次に、UIViewRepresentableをフックしてUIProgressView、ビューのobservedProgressプロパティを前述の静的リクエスト オブジェクトのprogressプロパティに設定します。

struct ProgressBar: UIViewRepresentable {
    
    func makeUIView(context: Context) -> UIProgressView {
        let progressView = UIProgressView()
        
        // Use the static NSBundleResourceRequest:
        progressView.observedProgress = OnDemandResources.request?.progress
        
        return progressView
    }

    func updateUIView(_ uiView: UIProgressView, context: Context) {

    }
}

documentationによると、プロパティを設定するobservedProgressと、進行状況の値が自動的に変更されるはずです。

このプロパティが設定されている場合、進行状況ビューは、Progress オブジェクトから受け取った情報を使用して進行状況の値を自動的に更新します。

最後に、次ProgressBarのように SwiftUI ビューでmy を利用しています。

struct MyView: View {
    var body: some View {
        ProgressBar()
    }
}

結果:

進行状況バーには進行状況が表示されません。それは単なる空白のバーです。

質問:

ProgressBarODR ダウンロードの進行状況が表示されないのはなぜですか? 私は何を間違っていますか?これはそれを行う方法ですか、それとも簡単な方法はありますか?

ありがとうございました!

4

0 に答える 0