Using NSPrivateQueueConcurrencyType Child Context with NSPrivateQueueConcurrencyType Parent Context

I'm having problems using the child context as a notepad with the NSPrivateQueueConcurrencyType attribute.

My main data stack is as follows: core data stack

View controllers use the main context. The worker context is used to import data from the API. I use mergeChangesFromContextDidSaveNotification to merge the changes between the Main and Worker contexts. If I leave the working child context out of the equation, it seems to work correctly.

However, I would like to use the working child context as a notepad when importing objects. Some data requires the creation of nested objects, and if there is an error during the build process, I would just like to discard this context. If the assembly succeeds, I expect that I can save the working child context, make changes to the working context, which can then save and merge the changes into the main context.

However, when I try to execute a select query in the Childer Child Context to search or create, although it is executed inside the performBlock in the Childer Child Context, I get a multi-threaded statement.

I'm not sure which code snippets will be helpful in answering this question, but my main problem is that my general approach will not work. Does a private queue context as a child of another context have a bad idea?

EDIT:

The failure I encountered is when a working child context tries to execute a select query in order to perform a search or create operation. It does not use any managed objects in the predicate and performBlockAndWait it in performBlockAndWait . The explanation I get is a "NSInternalInconsistencyException", reason: "statement is still active . " The failure is intermittent, but so far it seems that this only happens when I have nested contexts of child employees. (i.e. the context of the working child in my diagram will have a child context of its own creation objects)

A fetch request that causes failures is always executed for a search or create operation, so it tries to get any objects with a unique identifier property corresponding to the identifier of the imported objects. Thus, the predicate is always similar to "identifier in ["1234", "abc", "etc" ]

As I mentioned in the commentary, I initially used the context setting PSC -> Private Context -> Main Context -> Private Worker. My user interface hangs when the working context retrieves and saves when importing data, so I'm trying to reorganize this stack to free up the user interface.

+5
source share
1 answer

Displaying your sample and its predicate will help determine what is happening. Looks like you're accessing NSManagedObject through thread boundaries, perhaps using one in your predicate?

By the way, if you move your workers to your main line of business, you won’t need to process consumption notifications. Everything else would be exactly the same, but you would have less code to solve and less risk of threading issues.

At first, I had workers in my main line. PSC β†’ Private Queue β†’ Main Queue β†’ Private Employees. I am refactoring the settings that I am asking because I am experiencing a UI lock when work contexts are retrieved / saved to search or create imports. Is there any other way to optimize the performance that you would recommend besides refactoring the main data stack?

Do you have tools to make sure the block is from a child MOC? In each case, I heard about this problem, I found that it was something else (usually related to the user interface) that was the actual source of the problem. If you do not load THOUSAND objects against a bad predicate, you should not feel hit for extraction. Even then, the predicate a will be optimized to solve the problem.

As for saving, you can either A) increase your save frequency, or B) set up a private MOC to write to disk. I prefer B so that all stored functions are asynchronous.

0
source

All Articles