Potential reference count issues unless grabbing fresh reference in background thread

前端 未结 1 1600
暖寄归人
暖寄归人 2021-01-26 18:08

I have a second question after reading Marcus S. Zarra\'s (excellent) Core Data: Data Storage and Management for iOS, OS X, and iCloud (2nd edition) if I may.

The book\

相关标签:
1条回答
  • 2021-01-26 19:03

    The block itself could be executed relatively late in this example (a lot of other code could execute before this block). Which means much can happen to the context or the store coordinator and hypothetically the store coordinator is not even the same object before and after the block starts executing.

    By calling the manager to retrieve the new reference for the coordinator you ensure first that you will get the latest coordinator as well as you keep the current coordinator unretained from the block. If you were to reuse the coordinator from outside the block that coordinator will be retained and may produce (although unlikely) issues such as memory inflating. And if all goes bad and the block is never even executed the coordinator is simply retained forever and you have a memory leak.

    This is just a good practice.

    0 讨论(0)
提交回复
热议问题