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:
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.