This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor final class ViewModel: ObservableObject { | |
private let list: FetchedResultList<Fruit> | |
init(context: NSManagedObjectContext = PersistenceController.shared.container.viewContext) { | |
list = FetchedResultList(context: context, | |
sortDescriptors: [ | |
NSSortDescriptor(keyPath: \Fruit.name, ascending: true) | |
]) | |
list.willChange = { [weak self] in self?.objectWillChange.send() } | |
} | |
var fruits: [Fruit] { | |
list.items | |
} | |
@Published var searchText: String = "" { | |
didSet { | |
if searchText.isEmpty { | |
list.predicate = nil | |
} | |
else { | |
list.predicate = NSPredicate(format: "name contains[cd] %@", searchText) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment