Skip to content

_CoreDataDependency doesn't update its Fetched item. #68

@tgrapperon

Description

@tgrapperon

Discussed in #67

Originally posted by m-housh May 19, 2023
Forgive my ignorance with CoreData in general, but I'm playing around with the _CoreDataDependency and trying to determine how to update a Fetched item. I have tried several approaches, mainly the following...

struct TodoFeature: Reducer {
  struct State: Equatable {
     var todos: Todo.FetchedResults = .empty
  }

  Action: Equatable {
    ...
    case toggleComplete(todo: Fetched<Todo>)
  }

  @Dependency(\.persistentContainer) var persistentContainer;

  var body: some ReducerOf<Self> {
    Reduce { state, action in
      switch action {
       ...
       case .toggleComplete(todo: let todo):
          todo.withManagedObject { update in 
            update.complete.toggle()
            try! update.managedObjectContext!.save()
          }
         return .none
      }
      
    }
  }
}

This does not update the view / state, however if I shut the app down and restart it, the complete value is toggled. So I'm not sure if I need to invalidate the cached items that have been fetched, but calling my task that initially loads the todo's does not seem to update the todo's state either. Once again, forgive my ignorance in working with core data in general as it seems like there's something basic that I'm missing.

Here's the repository for more complete example.

https://github.com/m-housh/CoreData_Test

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions