Saturday 15 June 2013

objective c - how to pass NSManagedObjectContext around in iOS app? -


I am using .xibs and I want to use core data. The easiest answer is to define @property in AppDelegate.h. But I have to fill my model in XML-Parser file and I want to get data in many controllers. Therefore, using [code] [[[UIApplication shared sharing] representative] context] is so ugly, is there a beautiful solution?

Remember that each object has subclass NSManagedObject reference also that your The pass (classic example) is book and author . This is Entitis ofc with Relationships 1-N (a writer - many books)

I do not know what you try to do here, but in many applications you have master-details-more details - ... viewcontrollers Back to the top:

Let's say that your main viewer is a writer view controller . It has a table view with authors. You want to tap on the author and see what books he has written. So you have a model property in your BooksViewController :

  @property (strong, nonatomic) author * writer;   

You can set it in many different ways. Custom Setter, Near from Segue ... Never, I'm not sure it's very clear.

What's great here is that after you do this, you already have a reference in your BooksViewController . Every time you write:

self.author.managedObjectContext will be the correct reference.

So when you have a navigation controller app, you can make a reference in AppDelegate or in singleton class or main value. You will call it only once, and then call it from NSManagedObject which you will pass.

No comments:

Post a Comment