开发者

How to pass NSManagedObjectContext through objects

I'm wondering which is the best way to pass NSManagedObjectContext in IOS Application. I used to add an NSManagedObjectContext property with retain (not assign) and create custom init method... but i'm not sure that this's a best practice. Here an example of a Class which need a NSManagedObjectContext.

@interface CatSelectVC : UIViewController {
    NSArray *catList;
    NSManagedObjectContext *context;
}
@property(nonatomic,retain) NSManagedObjectContext *context;
-(CatSelectVC*)initWithContext:(NSManagedObjectContext*) theContext;

and i implement custom init straightforward assigning theContext to self.context. context is synthesized...

-(CatSelectVC*)initWithContext:(NSManagedObjectContext*)theContext{
  self = [super init];

  if(self !=nil){ 
     self.context = theContext;
  }

  return self;
}

Is this a good method ? what about retain the context ? would开发者_高级运维 be a better solution to use assign instead of retain for context property ?


I tend to get the Context from a passed object.

NSManagedObjectContext *context = self.currentExercise.managedObjectContext;

I think you definitely want to retain the context.

EDIT:

I would suggest The book entitled "Core Data" by Marcus Zarra or the iDeveloperTV video with Marcus Zarra and Scotty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜