Posts

Post marked as solved
17 Replies
This really worked with great performance. Maybe not what I would expected from the framework, but until SwiftUI become more mature I think this would be an accepted answer.Final solution that worked great:struct NameList: View { @ObservedObject fileprivate var global = GlobalSettings() @FetchRequest( entity: Name.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)] ) var names: FetchedResults<Name> @State var selectedGender = Defaults.gender @State var filtered = [Name]() var body: some View { NavigationView { VStack { Picker("Gender", selection: $global.gender) { Text(Gender.female.rawValue.uppercased()) .tag(Gender.female) Text(Gender.male.rawValue.uppercased()) .tag(Gender.male) Text(Gender.unisex.rawValue.uppercased()) .tag(Gender.unisex) } .pickerStyle(SegmentedPickerStyle()) .padding() List( filtered, id: \.objectID) { (item: Name) in NameListRow(item: item) } } } .onReceive(Defaults.publisher(for: \.gender)) { (gender) in self.selectedGender = gender self.filterNames() } .onAppear { self.filterNames() } } fileprivate func filterNames() { self.filtered = [] DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { self.filtered = self.names.filter { $0.gender == self.selectedGender } } } }
Post marked as solved
17 Replies
Don't know if this is what you meant, but none of them worked:.id(item.objectID)List(filtered, id: \.objectID) { (item: Name) in NameListRow(item: item) .id(item.objectID) }.equatable() (not sure if I'm using it correctly)List(filtered, id: \.objectID) { (item: Name) in NameListRow(item: item).equatable() }struct NameListRow: View, Equatable { static func == (lhs: NameListRow, rhs: NameListRow) -> Bool { lhs.item.objectID == rhs.item.objectID } ... }
Post marked as solved
17 Replies
Unfortunately, using \.objectID as identifier does not improve performance at all.List(filtered, id: \.objectID) { (item: Name) in NameListRow(item: item) }I have tried to create a fetch request that change the predicate when toggle gender, but that does not improve performance either. It's not the fetch request or the filtering that takes time. It is the rendering, scanning the view hierarchy for changes as you mention.
Post marked as solved
17 Replies
By rendering the entire list and individually hide any rows that should be filtered out improves the performance to milliseconds, which is great. However, by returning nil I end up with an empty row, taking up space. And that is not what I want. Any solution to that ? ( I writing it exactly as your example)
Post not yet marked as solved
24 Replies
When I try to use this solution in Xcode 11 Beta 4, I receive an error with this code:List(store.items, id:\.objectID) { item in NavigationLink(destination: Detail(item: item)) { Row(item: item) } }.onAppear { self.store.performFetch() }"UITableView was told to layout its visible cells and other contents without being in the view hierarchy. ..." and"Invalid update: invalid number of sections. ..."However, changing the code to add a 0.1s delay before fetching fixes the issue:List(store.items, id:\.objectID) { item in NavigationLink(destination: Detail(item: item)) { Row(item: item) } }.onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { self.store.performFetch() } }I only have about 10 items in my store so the time for the fetch is very small. Could it be that your 50000 row fetch takes more than my 1ms delay so you don't experience this problem? Is there a better way to execute the fetch than "onAppear".