I have a Core Data publisher that is working great so far. I have a Workspace entity and a Project entity. I use the following publisher to get all the projects for a given workspace:
class ProjectModel: ObservableObject {
@Published var projects = [Project]()
private var cancellableSet: Set<AnyCancellable> = []
init(){
CoreDataPublisher(request: Project.getAllProjects(), context: PersistenceController.shared.container.viewContext)
.sink(
receiveCompletion: { print($0) },
receiveValue: { [weak self] items in
self?.projects = items
})
.store(in: &cancellableSet)
}
}
The fetch request getAllProjects() is in a Core Data entity extension here where the NSPredicate filters based on a Workspace object set in the UI.
//Core Data Entity Extension
extension Project{
@nonobjc public class func getAllProjects() -> NSFetchRequest<Project> {
let workspace = AppState.shared.workspace as Workspace //<-- The user can change this workspace
let request = NSFetchRequest<Project>(entityName: "\(Self.self)")
request.sortDescriptors = [NSSortDescriptor(keyPath: \Project.name, ascending: true)]
request.predicate = NSPredicate(format: "workspace = %@", workspace)
return request
}
}
This Workspace object is in a global state class:
class AppState: ObservableObject{
static let shared = AppState()
@Published var workspace: Workspace!
init(){
//Setup the workspace for the first time
}
}
I can successfully receive data from my publisher and I can successfully change the global Workspace in the UI. The problem is that after changing the Workspace, the publisher still points to the old Workspace originally set when the fetch request was created.
How do I prompt the ProjectModel to reinitialize in order to renew the publisher's state when the AppState's workspace is changed?