Reputation: 443
Why wouldn't I be able to use this draft
object within the view's body?
I've also tried:
mainQueueConcurrencyType
for the child contextextension NSManagedObjectContext {
public func newChildContext() -> NSManagedObjectContext {
let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
moc.parent = self
return moc
}
}
struct InternalContentView: View {
private var context: NSManagedObjectContext
@State private var draft: PlayerEntity
init(context: NSManagedObjectContext) {
self.context = context
let childContext = context.newChildContext()
draft = PlayerEntity.create(context: childContext)
print("draft: \(draft)")
print("draft id: \(draft.id?.uuidString ?? "unknown id")")
print("draft is faulted: \(draft.isFault)")
}
var body: some View {
printv("draft: \(draft)")
printv("draft id: \(draft.id?.uuidString ?? "unknown id")")
printv("draft is faulted: \(draft.isFault)")
Text("")
}
}
The output is:
draft: <PlayerEntity: 0x600001de6170> (entity: PlayerEntity; id: 0x600003e4fc60 <x-coredata:///PlayerEntity/t54BF6346-6299-479F-AD02-AC61AC056CCF2>; data: {
email = nil;
games = (
);
id = "51BDEE17-DCA4-4EB9-8A56-0780ACFF55B3";
"name_" = nil;
teams = (
);
})
draft id: 51BDEE17-DCA4-4EB9-8A56-0780ACFF55B3
draft is faulted: false
draft: <PlayerEntity: 0x600001de6170> (entity: PlayerEntity; id: 0x600003e4fc60 <x-coredata:///PlayerEntity/t54BF6346-6299-479F-AD02-AC61AC056CCF2>; data: <fault>)
draft id: unknown id
draft is faulted: true
Upvotes: 0
Views: 201
Reputation: 52033
Right now your child context only lives within the init
but you will need to have access to it outside as well (or rather at least until you call save()
on it)
So make it a property instead
struct InternalContentView: View {
private var context: NSManagedObjectContext
private var childContext: NSManagedObjectContext
...
init() {
self.context = context
childContext = context.newChildContext()
Upvotes: 1