UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

CoreData retrieving wrong item in List

Forums > SwiftUI

Hi,

I am relatively new to SwiftUI and CoreData.

I have a Server CoreData object that holds user inputs for a Server that they enter the details of in a form:

let newServer = Server(context: self.managedObjectContext)
                    newServer.id = UUID()
                    newServer.name = self.serverName
                    newServer.ip = self.ipAddress
                    // unwrapping as portNumber CANNOT be empty
                    newServer.port = Int32(self.portNumber)!
                    do {
                      try self.managedObjectContext.save()
                      print("Server saved.")
                      self.presentationMode.wrappedValue.dismiss()
                     } catch {
                      print(error.localizedDescription)
                      }
                    }) {
                    Text("Save")

In another View, I use a FetchRequest to retrieve the servers:

// FetchRequest is to retrieve core data for list
@FetchRequest(entity: Server.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Server.name, ascending: true)]) var servers: FetchedResults<Server>

Then finally a list to show the Servers in a list:

var body: some View {
            NavigationView {
                VStack {
                    // Conditional view in case Server list is empty
                    if !servers.isEmpty {
                    List {
                        ForEach(servers) { server in
                            NavigationLink(server.name! ,destination: HomeView(isRootActive: self.$isActive, model: model).onAppear {
                                print(server.name)
                                model.load(server: server)
                            }, isActive: self.$isActive)

                        }
                        ...

This works fine for when there is only one item in the list. However, when I add more than one item, to the database and try to click on it to take me to the next page, it uses the incorrect server to the name that is is presenting.

For example, If there are two items in the list, say:

  • Blah
  • Test

When clicking on Blah it will actually then do model.load on Test, whereas clicking on Test will do model.load on Blah.

Any ideas on why this happening? I am using Xcode beta 2 currently. I can provide more context if more context is needed.

Thanks.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.