Multithreading and NSNotification in iOS -
i wrote few functions in appdelegate.m this:
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { debugmethod(); self.weibostatusesdatabasecontext = [self createmainqueuemanagedobjectcontext]; return yes; } - (void)setweibostatusesdatabasecontext:(nsmanagedobjectcontext *)weibostatusesdatabasecontext { debugmethod(); _weibostatusesdatabasecontext = weibostatusesdatabasecontext; nsdictionary *userinfo = self.weibostatusesdatabasecontext ? @{weibostatusesdatabaseavailabilitycontext : self.weibostatusesdatabasecontext} : nil; [[nsnotificationcenter defaultcenter] postnotificationname:weibostatusesdatabaseavailabilitynotification object:self userinfo:userinfo]; }
and wrote function in mainpagecdtvc.m coredata tableviewcontroller :
- (void)awakefromnib { debugmethod(); [[nsnotificationcenter defaultcenter] addobserverforname:weibostatusesdatabaseavailabilitynotification object:nil queue:nil usingblock:^(nsnotification *note) { self.managedobjectcontext = note.userinfo[weibostatusesdatabaseavailabilitycontext]; debuglog(@"context = %@", self.managedobjectcontext); }]; debuglog(@"context = %@", self.managedobjectcontext); }
when set mainpagecdtvc initial viewcontroller , run app, got result xcode console :
2015-04-02 11:37:38.650 weibodemo[962:48379] -[mainpagecdtvc awakefromnib] 2015-04-02 11:37:38.665 weibodemo[962:48379] context = (null) 2015-04-02 11:37:38.669 weibodemo[962:48379] -[appdelegateapplication:didfinishlaunchingwithoptions:] 2015-04-02 11:37:38.673 weibodemo[962:48379] file:///users/wlsong/library/developer/coresimulator/devices/7dccff56-7a91-4dcc-bb20-ce33fcae42bf/data/containers/data/application/82c32b6f-a842-4bfd-a9df-0126a7c290bc/documents/ 2015-04-02 11:37:38.691 weibodemo[962:48379] managedobjectcontext have created. 2015-04-02 11:37:38.691 weibodemo[962:48379] -[appdelegate setweibostatusesdatabasecontext:] 2015-04-02 11:37:38.695 weibodemo[962:48379] context = < nsmanagedobjectcontext: 0x7b84da00 >
my questions are:
- why did "context = < nsmanagedobjectcontext: 0x7b84da00 >" appear in last?
- are -[appdelegate application:didfinishlaunchingwithoptions:] , -[mainpagecdtvc awakefromnib] in same thread?
- if run first?
ps: chinese, english poor. forgive me.
the order is:
awakefromnib
applicationwillfinishlaunching
applicationdidfinishlaunching
you don't have worry multi-threading/parallelism here. if follow order see:
awakefromnib
=> defaultcenter observer add => print (null) contextapplicationdidfinishlaunching
=> call setter => setter posts notification => print context
Comments
Post a Comment