Passing managedObjectContext along to view/controller hierarchy

前端 未结 2 1506
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 02:37

Managed object context in most cases created in application delegate. What are the ways of passing it along to view/controller hierarchy, so every navigation/tab controller in t

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 03:14

    The recommended design is to do a dependency injection. This means that when you create a UIViewController you pass in the NSManagedObjectContext via a property. If the UIViewController only needs a single or set of NSManagedObject instances then you would just hand those in via properties.

    This design is more flexible and allows for greater reuse of your code. Linking everything to a single "hard-coded" NSManagedObjectContext in the AppDelegate is fragile and reduces the flexibility of your code.

    I discuss this in depth on The Mac Developer Network. (edit: lost link now via Wayback Machine's Cache)

提交回复
热议问题