I looked everywhere for a solution to my problem, but I couldn't find any. There's this question that is similar, but I think I'm having a different problem here. So, my code (Xcode 12.1, developing for iOS 14.0) looks like this:
import SwiftUI
struct ContentView: View {
@ObservedObject var cm : FolderModel //Which is conformed to Codable, Identifiable, Equatable, ObservableObject
@ObservedObject var dm : DataManager //Which is conformed to Equatable, Identifiable, ObservableObject
@State var pressedFolder = false
@State var valview : ValuesView
NavigationView {
VStack{
ScrollView(.horizontal) {
HStack { ForEach(dm.storageFolders) { foldersForEach in
Button(action: {
valview = ValuesView(cm: foldersForEach, dm: dm)
pressedFolder = true
}, label: {
Text(foldersForEach.folderName)})
}
if pressedFolder == false {
Form {
ForEach(dm.values) { passwordDelForEach in
NavigationLink(//This works correctly)
}
}
} else if pressedFolder == true {
valview //This is the thing that it's not updating when values are added to the folders
}
}
struct ValuesView : View {
@ObservedObject var cm : FolderModel //Which is conformed to Codable, Identifiable, Equatable, ObservableObject
@ObservedObject var dm : DataManager //Which is conformed to Equatable, Identifiable, ObservableObject
var body : some View {
Form {
ForEach (cm.folderValues) { folderValuesForEach in
NavigationLink(//This works correctly)
}
}
}
}
The arrays into the DataManager are all declared like this:
@Published var storage : [StorageModel] = [] {
didSet {
objectWillChange.send()
}
}
typealias Storage = [StorageModel]
If I add anything into the arrays (from another View), data is stored correctly because by opening the .plist file (that the DataManager creates) I can see it gets correctly updated. Plus, every Button that I use has either the func of the DataManager save() (which has objectWillChange.send() within it) or I manually add dm.objectWillChange.send() to the action of the Button. Despite all this, the things into the ForEach don't update. I only see the things that were there the first time I open the app, and to see the changes I have to close the app and reopen it.
What am I doing wrong?
Thanks to everyone who will answer!