Reputation: 18670
This is a bit of a tricky one.
I have Document
entities that are currently being imported into CoreData from a SQLite database on a background thread. There's a separate context for the background thread and I am batching the save at every 500 entries.
Saving the background thread context triggers a notification which grabs my main thread's contexts and performs a merge between the two.
Everything works as expected if I am only importing document
entities.
My problem occurs when I try and establish a relationship between the current document
being created, an another entity called briefcase
.
This is what my import routine currently does:
Briefcase
entityDocument
entities for each rowContextSave
notification which grabs the main thread and merges with the main thread's context.Briefcase
entity gets merged with the main thread so when my loop continues, the next document entity created tries to associate itself with the briefcase, which is where I get a crash saying I can't establish relationships between objects on separate threads. I know that if I remove the call to reset the context after saving it, everything works as expected but my memory footprint goes way up and it is not something I am prepared to accept.
So my question is:
Can you think of a way of keeping the Briefcase entity around (and valid) for the entire import process so I can continue to create the relationships?
My first thought was to create the briefcase entity without a context and then add it to the context once the whole process is finished. This didn't work very well (it crashed on creation).
Your thoughts are very much appreciated. Rog
Upvotes: 0
Views: 780
Reputation: 18670
Answering my own question:
Upvotes: 1