Why does the SwiftUI Picker not show the correct selected value if it's set using .onAppear
Asked Answered
E

1

3

Background

I want a view that will allow the user to create a new core data item. That item will have a link to another core data item which the user will select from a picker which will be pre-populated with a default item selected when the view first loads (the most relevent one for that user).

## Problem

But when I change the pickers "selection" variable during the .onAppear (in the actual code it's a modified version on .onAppear that only runs the code the first time .onAppear shows but that's good enough for this question) it doesn't change the picker's selected value.

If I then change it again using say a button that runs the same selection code it DOES change the picker.

Also if I add something that references the picker's selection var (which is a @State. See example code) it works perfectly!

How to replicate the issue

To replicate the issue create a new project in Xcode (14.3), select App and tick Use Core Data.

Now replace the whole of the ContentView with the following code:

import SwiftUI
import CoreData

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: []
    ) var items: FetchedResults<Item>
    
    @State private var selectedItem: Item?
    
    var body: some View {
        Form {
            // ---------------------------------------------
            // Uncommenting the next line fixes the pickers!
            //let _ = selectedItem
            // ---------------------------------------------
            Picker ("Part of Item Session", selection: $selectedItem) {
                Text("New Item").tag(nil as Item?)
                ForEach(items) { item in
                    Text("\(item.timestamp!.description)").tag(item as Item?)
                }
            }
            Button {
                ContentView.addItems(withViewContext: viewContext)
            } label: {
                Text("Add Items")
            }
            Button {
                selectedItem = ContentView.getItem(withViewContext: viewContext)
            } label: {
                Text("Select Random Item")
            }
            Button {
                if (selectedItem != nil) {
                    print("Item: \(String(describing: selectedItem!.timestamp))")
                } else {
                    print("Item is nil!")
                }
            } label: {
                Text("View Item (print to console)")
            }
        }
        .onAppear {
            selectedItem = ContentView.getItem(withViewContext: viewContext)
            if (selectedItem != nil) {
                print("Item: \(String(describing: selectedItem!.timestamp))")
            } else {
                print("Item is nil!")
            }
        }
    }
    
    static func addItems(withViewContext viewContext: NSManagedObjectContext) {
        for _ in 0..<10 {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date().addingTimeInterval(-Double(Int.random(in: 1..<5000)))
        }
        do {
            try viewContext.save()
        } catch {
            let nsError = error as NSError
            fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
        }
    }
    
    static func getItem(withViewContext viewContext: NSManagedObjectContext) -> Item? {
        let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()
        fetchRequest.sortDescriptors = []
        
        var items: [Item] = []
        do {
            // Fetch
            items = try viewContext.fetch(fetchRequest)
            
            // Pick a random(ish) item
            if (items.count > 0) {
                let randomInt = Int.random(in: 0..<items.count)
                print("Setting Item: '\(items[randomInt].timestamp!.description)'")
                // Return the random(ish) item
                return items[randomInt]
            }
        } catch {
            print("Unable to Fetch Item, (\(error))")
        }
        
        return nil
    }
}

Then:

  • Running the project in an iPhone 14 Pro Max simulator
  • Click the Add Items button to add some examples to your DB
  • Re-run the app in the simulator from Xcode to re-start it and you'll see the picker still has New Item selected but if you click the View Item ... button it'll print the selection's actual value which is not nil (the value for New Item)!
  • If you change the selection use Select Random Item (which just picks another random item for the picker) it will correctly change the Picker correctly.

Also if you:

  • Restart the app again
  • Click the View Item button
  • Select the same item from the picker that was printed in the console it won't change the picker.
  • IF however you change the picker to any other item it WILL change the picker!

Strange hack that fixes it

To see the picker working like I expected it to un-comment the line let _ = selectedItem (line 17) and re-run the app... Now right away the picker is correct!

Question

What's going on here. Is it a bug in Swift or am I doing something wrong?

Eberta answered 3/5, 2023 at 13:43 Comment(2)
I suspect that Core Data isn't filling the fault without the let _ = selectedItem. I am curious as to why you are fetching selectedItem from the DB when you already have all of the items fetched with items? That seems redundant.Thrips
@Thrips the code is an example I created to show the issue easily in one file. The actual code in the static funcs etc are part of other files and re-usable code, not really in the view. :)Eberta
C
3

SwiftUI only calls body for states that are set if they have previously been read, ie their getter has been called. This is SwiftUI's dependency tracking system. Your way of doing a fake read is fine; here is another way:

.onAppear { [selectedItem] in
Cottrill answered 4/5, 2023 at 6:35 Comment(2)
OK makse sense it's not subscribing if it doesn't think it's been read but why doesn't the Picker call the get if it's using it in the selection? Also what is that .onAppear { [selectedItem] in doing? Thanks @malhal!Eberta
You pass $selectedItem which is same as Binding(get:{ } set { }) ie a pair of closures so no read happens until the get closure is called which is not tracked as a dependency. [] in is called a capture list part of closure syntax.Cottrill

© 2022 - 2024 — McMap. All rights reserved.