I have a model that has a unique property, e.g.:
@Model
final class UserWord
@Attribute(.unique)
let word: String
let partOfSpeech: PartOfSpeech
let metaData: ...
At the end of the init I have this:
init(...) {
if partOfSpeech == .verb {
metaData = fetchMeta()
}
}
This works fine when a word is newly created and saved. But let's say there's a unique conflict and a user tries to save a new entry with the same word
. Apparently this init still fires and fetchMeta
edits the existing entry which gives me the error:
CoreData: error: Mutating a managed object 0xb890d8167c911ade <x-coredata://4C75194F-D923-477F-BB22-ACBDECCD7530/UserWord/p2> (0x600002170af0) after it has been removed from its context.
I think the solution here is to do some manual checking of the modelContext
before saving. Would love to hear other's thoughts.