2

I've come across some puzzling behaviour of the SwiftUI List view. The List view is called with a binding that already contains some selected items. I know they exist. My data is longer than what can be shown on the screen. All the selected items on the part that is visible already are displayed as expected but when I scroll down the list to look for the other ones, they do not show straight away. I have to wait several minutes before they show.

Here is the code that generates the List:

import SwiftUI

struct ArtistView: View {

    @State private var editMode = EditMode.active
    @Binding var selection : Set<String>
    var artists = [String]()

    var body: some View {

        List(selection: self.$selection) {
            ForEach (self.artists, id: \.self) {artist in
                Text(artist)
            }
        }
        .environment(\.editMode, self.$editMode)

    }
}

Here is the code that calls the ArtistView:

NavigationLink(destination: ArtistView(selection: self.$selection, artists: self.settings.artists)) {
                                Text("Select Artists:")
                        }

Has anyone seen this kind of behaviour and more importantly is there a way to speed this display issue up?

Thanks

Kaiser
  • 31
  • 1
  • I think it is not good idea to have state for selection outside of view having corresponding List as it is responsible for this List update. It has to be not binding but state in same view or in some dedicated view model. – Asperi Apr 27 '20 at 18:14
  • Thanks, but my understanding is that a multi-selection list the selection parameter has to be a binding of a set. I tried different permutations on how to pass the binding. Essentially I need the selection in an environment variable but also suspected that that could be an issue. Hence I passed it to the view and into a local binding. I believe if seen this in examples but could be wrong. – Kaiser Apr 27 '20 at 18:21
  • The answer from [this topic](https://stackoverflow.com/a/61451928/12299030) should be helpful – Asperi Apr 27 '20 at 18:34
  • Thanks again, I tried that but still get the same behaviour. As mentioned, the selection itself works and is displayed properly at time of the selection. I am saving the selection and leave the view but when I return to the list and pass it the previous selection, not all of the selected items show as selected even though they are in the Set. The strange thing is that its precisely the items that are further down in the list and thus off screen. After quite a while (and I mean quite a while) they show as selected eventually. Why this delay? – Kaiser Apr 27 '20 at 19:19
  • I have experienced the same issue that when selected items go off-screen they seem to be unselected (but they are not). Did you find any solution to this behaviour? – Entalpi Jan 26 '21 at 20:18

0 Answers0