I can't access a newly saved object using the objectID.
I've created a child managed object context (cmoc) from my managedContext, and created a new object inside this cmoc. Once I've inputted the variables into the new object, I have called save() on the child context, save on the parent context, and then attempted to load the object in the parent context (and in my attempts in another background context) using the objectID. This has failed and I don't know why.
I've ensured that the saving is occurring before the attempt to return the object in the new context, and that hasn't helped. I've made sure I'm saving the correct cmoc and parent context. I'm a bit at a loss.
To create the childContext, object instance of person (of type Person) and pass to the next vc (destVC):
let childContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
childContext.parent = coreDataStack.managedContext
let person = Person(context: childContext)
destVC.coreDataStack = coreDataStack
destVC.person = person
destVC.childContext = childContext
Then variables added to the person instance
person.cellPhone = ... [etc]
I suspect my problem is in the save code, which currently states:
guard childContext.hasChanges else {
return
}
do {
try self.newKeyNumberContext.save()
} catch let error as NSError {
[Manage error]
}
coreDataStack.saveContext()
Or alternatively in the code immediately afterwards:
let personObjectID = self.person.objectID //(does return an objectID)
let personToReturn = try? coreDataStack.managedContext.existingObject(with: personObjectID)
which doesn't return an object, returning nil instead.