ios7 - Is there a better way to define NSManagedObjectContext definitions for MagicalRecord? -
in every method in every class of ios app, exception of appdelegate, have following line of code:
nsmanagedobjectcontext *localcontext = [nsmanagedobjectcontext mr_contextforcurrentthread];
in appdelegate.h file have this:
nsmanagedobjectcontext *localcontext;
and in appdelegate.m file have this:
localcontext = [nsmanagedobjectcontext mr_contextforcurrentthread];
i read in should have 1 such line of code in appdelegate, , have multiple references of other classes/methods.
if leave line of code out of of classes exception of appdelegate, accomplishing that, best way it?
update code added appdelegate.h file:
@property (strong, nonatomic) nsmanagedobjectcontext *managedobjectcontext; @property (strong, nonatomic) nsmanagedobjectcontext *localcontext;
this code added appdelegate.m file:
// set default magicalrecord context in view controllers uitabbarcontroller *tbc = (uitabbarcontroller *)self.window.rootviewcontroller; uinavigationcontroller *nc = tbc.viewcontrollers[0]; calendarviewcontroller *cvcontroller = (calendarviewcontroller *)nc.topviewcontroller; cvcontroller.localcontext = self.managedobjectcontext; nc = tbc.viewcontrollers[1]; weeksapptsviewcontroller *wacontroller = (weeksapptsviewcontroller *)nc.topviewcontroller; wacontroller.localcontext = self.managedobjectcontext; nc = tbc.viewcontrollers[2]; customersetupcontroller *cscontroller = (customersetupcontroller *)nc.topviewcontroller; cscontroller.localcontext = self.managedobjectcontext; nc = tbc.viewcontrollers[3]; appointmentsviewcontroller *avcontroller = (appointmentsviewcontroller *)nc.topviewcontroller; avcontroller.localcontext = self.managedobjectcontext; nc = tbc.viewcontrollers[5]; settingsviewcontroller *scontroller = (settingsviewcontroller *)nc.topviewcontroller; scontroller.localcontext = self.managedobjectcontext;
when run app in debugger, crashes on line of code:
calendarviewcontroller *cvcontroller = (calendarviewcontroller *)nc.topviewcontroller;
with following message:
* terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[calendarviewcontroller viewcontrollers]: unrecognized selector sent instance 0xb67ed40'
if entire app uses one managed object context, , sure never change in future, use
nsmanagedobjectcontext *localcontext = [nsmanagedobjectcontext mr_defaultcontext];
in view controllers.
but introduction of nested managed object contexts, apple recommends in core data release notes os x v10.7 , ios 5.0:
nested contexts make more important ever adopt “pass baton” approach of accessing context (by passing context 1 view controller next) rather retrieving directly application delegate.
in simplest case, pointer default context in appdelegate:
defaultcontext = [nsmanagedobjectcontext mr_defaultcontext];
and pass context appdelegate first view controller, , there next view controllers (e.g. in prepareforsegue
when using storyboards), , on.
that makes transition easier if decide later change structure, e.g. multi-context described here: http://www.cocoanetics.com/2012/07/multi-context-coredata/
note according answer https://stackoverflow.com/a/20536335/1187415, mr_contextforcurrentthread
deprecated.
Comments
Post a Comment