Can I make NSManagedObject in singleton?

I have an object NSManagedObject

filled with data that I want to use across multiple controllers. Can I make this object in a singleton and use it across multiple viewers? Or should I use a different approach?

+2


source to share


3 answers


As an alternative to a singleton, consider creating it in the application's delete, initialized when the application finishes running.



In your view controller, set a reference NSManagedObject

to this property value when instantiating the view.

+2


source


You've already gone around NSManagedObjectContext. You can use this to get the required data at any time.

I don't know how Core Data will react when you make one instance of this item. For example, NSmanagedObject does not use the same initialization methods as NSObject.

It uses -awakeFromInsert and -awakeFromFetch. So you already have a problem.



See this article by Marcus Zarra (Core Data Guru).

In short, just do a fresh fetch to get the data you want, no need to work with a single.

+2


source


It depends on why you want to make it singleton, if you cannot manage to pass it to all entities that need to access the data, using a singleton solution is not a good solution anyway. This usually creates more problems than solving any problems.

If you are concerned about multiple changes to the same object, Core Data has mechanisms to fix it, see the Change Management chapter in the Master Data Programming Guide

0


source







All Articles