Core Data privateQueue performBlockAndWait deadlock while accessing relationship

前端 未结 2 1804
南笙
南笙 2021-02-06 00:55

This topic has been discussed at many forum, but I am still not able to fully understand how performBlockAndWait actually works. As per my understanding, cont

2条回答
  •  别那么骄傲
    2021-02-06 01:19

    1. What are the "standard" messages?

    Any message sent to the managed object context, or any managed object. Note that the documentation continues to clarify...

    There are two exceptions:
    
    * Setter methods on queue-based managed object contexts are thread-safe.
      You can invoke these methods directly on any thread.
    
    * If your code is executing on the main thread, you can invoke methods on the
      main queue style contexts directly instead of using the block based API.
    

    Thus, anything but a setter method on a MOC must be called from inside a performBlock. Any method on a MOC that is of NSMainQueueConcurrencyType may be called from the main thread without being wrapped inside a performBlock.

    1. Can we set properties of a managed object which is fetched inside performBlock* API of a context outside performBlock*?

    No. Any access of a managed object must be protected from inside performBlock on the managed object context in which the managed object resides. Note the exception for managed objects residing in a main-queue MOC being accessed from the main queue.

    1. Why performBlockAndWait is misbehaving and causing UI block in my test code.

    It is not misbehaving. performBlockAndWait is reentrant, but only when already processing a performBlock[AndWait] call.

    You should never use performBlockAndWait unless you have no other option. It is especially problematic with nested contexts.

    Use performBlock instead.

提交回复
热议问题