After receiving update from CloudKit SwiftUI doesn't update child view, but does update if child view is inlined

79 Views Asked by At

This works as expected after receiving an update from CloudKit:

@Model
final class File {
    var name: String = "" // <- I edit the name on another device in the simple text editor, the editor code is ommited as it's not significant

    init(name: String) {
        self.name = name
    }
}

struct ContentView: View {
    @Query(sort: \File.name) private var files: [File]

    var body: some View {
        NavigationStack {
            ForEach(files) { file in
                NavigationLink(value: file) {
                    Text(file.name) // <- The text updates successfully after CloudKit sync
                }
            }
        }
    }
}

Here is the same app, but with extracted sub-views:

struct ContentView: View {
    @Query(sort: \File.name) private var files: [File]

    var body: some View {
        NavigationStack {
            FilesView(files: files) // <- Just wrapped the code into a separate view
        }
    }
}

struct FilesView: View {
    var files: [File]

    var body: some View {
        ForEach(files) { file in
            NavigationLink(value: file) {
                FileTileView(file: file) // <- Another extracted view
            }
        }
    }
}

struct FileTileView: View {
    var file: File

    var body: some View {
        Text(file.name) // <- Doesn't get updated when CloudKit received the sync update
    }
}

The code is the same, just couple of extracted views. Why doesn't it work?

0

There are 0 best solutions below