I'm noticing when adding items to a List they are out of order. The items are being added using Core Data, in my main View I show a sheet and then a user selects an item it's added to my main List. I assumed the items would be added in some kind of order from first to last.
My main view fetching CoreData items
@FetchRequest(sortDescriptors: [], animation: .default)
private var items: FetchedResults<Items>
List {
ForEach(items, id: \.self) { item in
Text(item.city)
}
}
Core Data saving items in the sheet view.
func addCity(_ city: String) {
let fetchrequest: NSFetchRequest<Items> = Items.fetchRequest()
fetchrequest.predicate = NSPredicate(format: "city = %@", city)
do {
let items = try viewContext.fetch(fetchrequest)
if !items.isEmpty {
presentationMode.wrappedValue.dismiss()
print("item exists")
} else {
let items = Timezone(context: viewContext)
items.city = city
try? viewContext.save()
presentationMode.wrappedValue.dismiss()
}
} catch {
print("error")
}
}

NSSortDescriptorfor the key but that just orders it alphabetically.