I have a Firebase app where I'm fetching a bunch of JSON from a remote database and then decoding and showing it. This works - but it won't actually fetch until a button is clicked in my app. Before any button is clicked, the screen displayed is just the default. After I click a button and go back, the entire decoded list shows up. Is there a way to get the list to show up without having to show anything? Also, I'm technically using a ForEach
in a ScrollView
because of UI instead of an actual List
. My code is below.
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()
}
}
and then my code for the actual fetching:
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")]
}
}
}
CodePudding user response:
Solved. I was fetching my data, but it took about a half a second - I showed a loading screen during that time, and then showed the updated view.