iphone - NSManagedObjectContext, best way to pass it around? Access it? -


i have question regarding how pass around nsmanagedobjectcontext. in app, seems it's appdelegate handles nsmanagedobjectcontext, shouldn't create other nsmanagedobjectcontexts in other viewcontrollers.

so question is...

there convention or smart method this?

thanks.

the way pass nsmanagedobjectcontext have ivar in each view controller pass to. modify initialiser include assignment, this....

mynewviewcontroller.h

 @interface mynewviewcontroller : uiviewcontroller {      nsmanagedobjectcontext *managedobjectcontext;  }  ... 

mynewviewcontroller.m

@implementation mynewviewcontroller  - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil andcontext:(nsmanagedobjectcontext *)ctx {     self = [super initwithnibname:nibnameornil bundle:nibbundleornil];     if (self) {         managedobjectcontext = ctx;     }     return self; }  ....        

then when call view controller, use modified initialiser. like...

mynewviewcontroller *nv = [[mynewviewcontroller alloc] initwithnibname:@"mynewviewcontroller" bundle:nil andcontext:self.managedobjectcontext]; 

now you've got reference managedobjectcontext can use within view controller.


Comments