This project was super helpful. I didn't understand much of what I read about Combine with Core Data until I looked through your code. Thank you!
I have a question for you. Let's pretend you have another Entity in Core Data called Project
. A user can select various projects and view Item
s within each (one Project
, many Item
s). When they switch the project in the UI, it should update which project is referenced in an NSPredicate
here:
extension Item {
// MARK: - NSFetchRequests for Items
@nonobjc public class func fetchItemsAll() -> NSFetchRequest<Item> {
let name = "\(Self.self)"
print("Running fetch request \(name) for all")
let request = NSFetchRequest<Item>(entityName: name)
request.sortDescriptors = []
request.predicate = NSPredicate(format: "project = %@", project) //<-- predicate here
return request
}
}
It seems that once the publishers are set up, they are only set to use whatever is initially defined for project
. But if the user changes it, I need the publisher to execute again, perform a new fetch request, and deliver the updated data to my views.
class AppState: ObservableObject{
static let shared = AppState()
@Published var project: Project!
}