单击按钮之前不会获取远程配置

Remote Config Won't fetch until button is clicked

我有一个 Firebase 应用程序,我从远程数据库中获取一堆 JSON,然后对其进行解码和显示。这有效 - 但在我的应用程序中单击按钮之前它不会真正获取。在单击任何按钮之前,显示的屏幕只是默认屏幕。单击按钮并返回后,会显示整个解码列表。有没有办法让列表显示而无需显示任何内容?此外,由于 UI 而不是实际的 List,我在技术上使用 ScrollView 中的 ForEach。我的代码如下。


import SwiftUI

struct PreviousBuglesView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @StateObject private var remoteConfig = RemoteConfig()
    @StateObject private var remoteConfig2 = RemoteConfig2()
//  @State var prevBugles = "Hi"
    @State var previousBugles = [bugle(date: "April 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"), bugle(date: "March 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")]
    let decoder = JSONDecoder()
    
    var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: true) {
                ZStack {
                    Rectangle()
                         .ignoresSafeArea()
                         .foregroundColor(Color("LightBlue"))
                     //    .navigationBarHidden(true)
                         .toolbar {
                             ToolbarItem(placement: .navigationBarLeading) {
                                 Button {
                                     presentationMode.wrappedValue.dismiss()
                                 } label: {
                                     Image(systemName: "chevron.backward.square.fill").onAppear(perform: {
                                        
                                     }).task {
                                         
                                         await remoteConfig.refreshConfig()
                                         await remoteConfig2.refreshConfig()
                                     }
                                         .font(.system(size: UIScreen.main.bounds.height * 0.05))
                                         .foregroundColor(Color("DarkBlue"))
                                       //  .offset(x: 0, y: UIScreen.main.bounds.height * -0.00)
                             }
                             }

                         }
                    VStack {
                       
                        Spacer()
                          
                            Text("Previous Bugles") .foregroundColor(Color("DarkBlue"))
                            .font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.1))  //.padding(.top, UIScreen.main.bounds.height * 0.015)
                            .multilineTextAlignment(.center)
                            //.padding()
                            Spacer()
                         //   .navigationBarHidden(true)
                        
                        ForEach(previousBugles, id:\.self){bugle in
                            
                            
                            NavigationLink {
                                PDFSwiftUIView(StringToBeLoaded: bugle.url)
                                    .navigationTitle(bugle.date + " Bugle")
                                    .navigationBarHidden(false)
                            } label: {
                                ZStack {
                                    RoundedRectangle(cornerRadius: 100)
                                        .frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.15)
                                        .foregroundColor(Color("DarkBlue"))
                                    .opacity(0.8)
                                    Text(bugle.date + " Bugle").font(Font.custom("Copperplate", size: UIScreen.main.bounds.height * 0.05))  .padding(.top, UIScreen.main.bounds.height * 0.015)
                                        .foregroundColor(.white)
                                        .padding()
                                }
                            }
                            Spacer()
                 }
                    //    Spacer()
                       
                        

                    }.offset(x: 0, y: UIScreen.main.bounds.height * -0.03)
                        .onAppear {
                                previousBugles = remoteConfig2.previousBugles
                        }
                }
            }.background(Color("LightBlue"))
                .task {
                    await remoteConfig.refreshConfig()
                    await remoteConfig2.refreshConfig()
                }
        }
    }
}

struct PreviousBuglesView_Previews: PreviewProvider {
    static var previews: some View {
        PreviousBuglesView()
    }
}

然后是我的实际获取代码:


import Foundation
import Firebase

class RemoteConfig: ObservableObject{
    @Published var thisMonthsBugle = "https://www.campwinadu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"
//    @Published var previousBugles = "Hello World"
    private var remoteConfig = Firebase.RemoteConfig.remoteConfig()
    
    init(){
        let settings = RemoteConfigSettings()
        settings.minimumFetchInterval = 0
        remoteConfig.configSettings = settings
    }
    func refreshConfig() async{
        guard (try? await remoteConfig.fetchAndActivate()) != nil else{return}
        
        //remoteConfig.fetcha
        
        
        await update()
       // await updateTwo()
    }
    @MainActor func update(){
        
        thisMonthsBugle = remoteConfig.configValue(forKey: "thisMonthsBugle").stringValue ?? "https://www.campwindu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"

      //  objectWillChange.send()
    }
}

class RemoteConfig2: ObservableObject{
    @Published var previousBugles = [bugle(date: "April 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"), bugle(date: "March 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")]
   
    private var remoteConfig = Firebase.RemoteConfig.remoteConfig()
    
    init(){
        let settings = RemoteConfigSettings()
        settings.minimumFetchInterval = 0
        remoteConfig.configSettings = settings
    }
    func refreshConfig() async{
        guard (try? await remoteConfig.fetchAndActivate()) != nil else{return}
        
        //remoteConfig.fetcha
        
        await update()
       // await updateTwo()
    }
    @MainActor func update(){
        let prev = remoteConfig.configValue(forKey: "previousBugles").stringValue ?? """
      [{
          "date": "MRCH 2022",
          "url": "https://www.campwinadu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"
        },
        {
          "date": "March 2022",
          "url": "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf"
        },
        {
          "date": "February 2022",
          "url": "https://www.campwinadu.com/wp-content/uploads/2022/02/Winadu-Bugle-Feb-2022-1.pdf"
        },
        {
          "date": "January 2022",
          "url": "https://www.campwinadu.com/wp-content/uploads/2022/01/Winadu-Bugle-Jan-2022.pdf"
        },
        {
          "date": "December 2021",
          "url": "https://www.campwinadu.com/wp-content/uploads/2021/12/Winadu-Bugle-Dec-2021.pdf"
        },
        {
          "date": "November 2021",
          "url": "https://www.campwinadu.com/wp-content/uploads/2021/11/Winadu-Bugle-Nov-2021.pdf"
        }]
"""
        let data = Data(prev.utf8)
        let decoder = JSONDecoder()
        do {
           previousBugles = try decoder.decode([bugle].self, from: data)
            
        } catch {
           previousBugles =  [bugle(date: "April 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/04/Winadu-Bugle-April-2022.pdf"), bugle(date: "March 2022", url: "https://www.campwinadu.com/wp-content/uploads/2022/03/Winadu-Monthly-Bugle-March-2022-1.pdf")]
        }
    }
}

已解决。我正在获取我的数据,但它花了大约半秒 - 在那段时间我显示了一个加载屏幕,然后显示了更新的视图。