Core Data singleton manager?

前端 未结 3 718
我在风中等你
我在风中等你 2021-02-01 06:39

What technical reasons are there not to make a singleton class to manage my Core Data? I\'m trying to make a decision now, if I should strip out all of the boilerplate core data

3条回答
  •  盖世英雄少女心
    2021-02-01 07:19

    The boilerplate code in the application delegate in the Xcode templates is functionally implemented as a singleton. The application object is a singleton and it maintains but one delegate object so you've only got one instances of the Core Data stack and since the application object is universally accessible, you can always get to the app delegate as well.

    However, even that works only for simple apps with one persistent store with all the context using that one store. In more complex apps you may have multiple stores or context so a singleton quickly becomes too bloated.

    A singleton usually won't buy you much complexity hiding or save duplicate coding because most of the coding you have to do with Core Data is in the controller layer where you link up the model to the view/interface. Since that logic is usually custom to each view, you can't actually park it in the singleton.

    I've used singletons in the past but in the end they usually prove more hassle than they are worth.

提交回复
热议问题