Skip to content

Error when trying to update entity #8

@oco-adam

Description

@oco-adam

Hi,

I've been trying to use the library, but I'm confused with some behaviour around updating entities.

In particular I'm getting an error of the form "Attempt to access an object not found in store", when I try and update an entity.

Here is a minimal example of the strange behaviour, which you should be able to reproduce easily in the context of the CoreDataRepositoryTests:

  func testUpdateMovie() async throws {
    let container = CoreDataStack.persistentContainer
    let repositoryContext = container.viewContext
    repositoryContext.automaticallyMergesChangesFromParent = true
    let repo = CoreDataRepository(context: repositoryContext)
    
    let movie = Movie(id: UUID(), title: "Some Movie", releaseDate: Date())
    
    // We can create a movie
    let createRes = await repo.create(movie)
    let createdMovie = try! createRes.get()
    
    // We can then fetch the movie, e.g. by its title
    let fetchRequest = NSFetchRequest<RepoMovie>(entityName: "RepoMovie")
    fetchRequest.predicate = NSPredicate(format: "title == %@", movie.title)
    let fetchRes: Result<[Movie], CoreDataRepositoryError> = await repo.fetch(fetchRequest)
    let fetchedMovie = try fetchRes.get().first!
    print("Fetched Movie", fetchedMovie)
    
    // But if we try and update the movie we get a failure
    var updatedMovie = createdMovie
    updatedMovie.title = "Updated title"
    let updateRes = await repo.update(updatedMovie.managedRepoUrl!, with: updatedMovie)
    let _ = try! updateRes.get()
    
//    Fatal error: 'try!' expression unexpectedly raised an error: 
//    CoreDataRepository.CoreDataRepositoryError.coreData(Error Domain=NSCocoaErrorDomain Code=133000 
//    "Attempt to access an object not found in store."
  }

It is quite possible that I'm just using the library incorrectly, so any assistance with what I'm doing wrong would be most appreciated.

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