I'm building a trip planning app that lets the user select a city from a local json file using a Picker.
The json file has 115K items and the UI freezes if I try to filter through this many items. Any suggestions on how to make this more efficient.
Any help would be greatly appreciated.
import SwiftUI
struct AddTripView: View {
let cities = Bundle.main.decode([Destination].self, from: "cities.json")
@State private var selectedCity = 0
@State var searchText = ""
var body: some View {
Form {
Section {
Picker(selection: $selectedCity, label: Text("Select city")) {
SearchBar(text: $searchText)
ForEach(cities.filter {searchText.isEmpty ? true : $0.city.lowercased().hasPrefix(searchText.lowercased())}, id: \.self) {
Text($0.city)
}
}
}
}.navigationBarTitle("Create trip")
}
}
struct AddTripView_Previews: PreviewProvider {
static var previews: some View {
AddTripView()
}
}
JSON example
[{
"id": 1,
"city": "Zaranj",
"country": "Afghanistan"
},
{
"id": 2,
"city": "Taloqan",
"country": "Afghanistan"
},
{
"id": 3,
"city": "Shīnḏanḏ",
"country": "Afghanistan"
},
{
"id": 4,
"city": "Shibirghān",
"country": "Afghanistan"
},
{
"id": 5,
"city": "Shahrak",
"country": "Afghanistan"
}]