Can you use NSManagedObject outside the executeBlock context?

The executeBlock: and executeBlockAndWait: methods have been added to NSManagedObjectContext to make concurrency easier to create. I use them - potentially quite naively - and I just realized that there is a question that I never asked.

If I subclass NSManagedObject inside one of the executeBlock methods, the stream "home" is the stream of its parent context, which in the case of NSPrivateQueueConcurrencyType is probably an independent stream, I have no other access.

So what do I need to make an executeBlock call just to access the data contained within my managed objects? Or is there some background magic that helps protect me when using getters? (Or setters, although this seems like a bad idea ...)

+6
source share
2 answers

NSManagedObject not intended to be used outside of its managedObjectContext thread / queue (once it works, and sometimes you crash ==> do not do this).

CoreData does not guarantee secure read access to an object.

To access an object belonging to the "private queue" context, always use either [context performBlock:...] or [context performBlockAndWait:...] unless you access its objectID or managedObjectContext properties.

+13
source

You need to use performBlock: or performBlockAndWait: but there is one exception. If you use NSMainQueueConcurrencyType and you use a managed object in the main queue, you can access it directly, without a block. This can be a great convenience when you need to update your user interface from a managed entity or vice versa.

+9
source

Source: https://habr.com/ru/post/945392/


All Articles