iPhoneã¢ããªä½æã¾ã§ã®éã®ãï¼ï¼
12. NSNotificationCenter
æå¾ã«NSNotificationCenterãç´¹ä»ããããããã³ã«ããªã²ã¼ããã¿ã¼ã³ã¯ä¸»ã«1対1ãKVOã¯1対1ã1対Nã«é©ç¨ãããã®ã§ããããNSNotificationCenterã使ãã¨ãN対Nã®ã¤ãã³ãä¼éãççµåã«å®ç¾ã§ããããã ããéç¥ã»ã³ã¿ã¼ã使ãæ¹æ³ã¯ãä»ã®2ã¤ã®æ¹æ³ããããªã¼ãã¼ããããä¼´ããã¨ã«ãªãããããããã¾ãæ´»ç¨ããã°ãããå ´é¢ã§ã¯ä»ã®2ã¤ã®æ¹æ³ãããããã©ã¼ãã³ã¹ãåä¸ãããå¯è½æ§ãæã£ã¦ããã
N対Nã¨ã¯è¨ã£ã¦ããããã¯å¥ã«ãããã³ã«ããªã²ã¼ããã¿ã¼ã³ã§ãå®ç¾å¯è½ãªãã¨ã§ãããè¤æ°ã®ãããã³ã«ãå®ç¾©ãã¦ãè¤æ°ã®ãããã³ã«ããªã²ã¼ããææããéä¿¡å ã¨ãè¤æ°ã®ãããã³ã«ãå®è£ ããããªã²ã¼ããç¨æããã°ãããã§N対Nã ãéç¥ã»ã³ã¿ã¼ã使ãã°ãããããã£ã¨ã¹ãã¼ãã«ãççµåã«å®ç¾ã§ããã¨ãã話ã§ããã
éç¥ã»ã³ã¿ã¼ã¯ãN対Nãå¯è½ã§ãããã¨ä»¥å¤ã«2ã¤ã®ã¡ãªãããæä¾ããã
①éåæå¦çPOSTãå¯è½
②éè¤ããä¸è¦ãªã¡ãã»ã¼ã¸ã®åé¤ãå¯è½
以ä¸ã«éåæPOSTã®ä¾ã示ãã
// NSNotificationã使ã£ãç°¡åãªã¤ãã³ãä¼éã®ãµã³ãã« static NSString* MySenderNotifyName = @"MySenderValueChanged"; // _xã®å¤ã10ã®åæ°ã«ãªã£ãã¨ãã«ç¾å¨ã®å¤ãéç¥ãã @interface Sender : NSObject { int _x; } @property(assign, nonatomic, readonly) int x; -(int)increment; @end @implementation Sender @synthesize x = _x; -(id)init { self = [super init]; if (self) { _x = 0; } return self; } -(int)increment { if (0 == ++ _x % 10) { NSNumber* val = [NSNumber numberWithInt:_x]; NSNotification* notification = [NSNotification notificationWithName:MySenderNotifyName object:val]; NSNotificationQueue* queue = [[NSNotificationQueue defaultQueue]; [queue enqueueNotification:notification postingStyle:NSPostNow/* NSPostWhenIdle */]]; // éåæãã¹ããå¿ è¦ãªãå ´åã¯ä»¥ä¸ã®å¼ã³åºãã§OK //[[NSNotificationCenter defaultCenter] postNotificationName:MySenderNotifyName object:self]; } return _x; } @end // Senderããã®éç¥ãåãåã£ããã³ã³ã½ã¼ã«åºåãè¡ã @interface Receiver : NSObject -(void)output:(NSNotification*)notification; @end @implementation Receiver -(void)output:(NSNotification*)notification { NSNumber* val = (NSNumber*)notification.object; NSLog(@"%d", val.intValue); } @end // ãã¹ãç¨ã¡ã¤ã³é¢æ° int main(int argc, const char** argv) { @autoreleasepool { // ReceiverãObserverã¨ãã¦éç¥ã»ã³ã¿ã¼ã«ç»é²ãã Receiver* receiver = [[[Receiver alloc] init] autorelease]; [[NSNotificationCenter defaultCenter] addObserver:receiver selector:@selector(output:) name:MySenderNotifyName object:nil]; // Senderã«å¯¾ãã¦ä½ãå¦çãè¡ã // æ¡ä»¶ãæºããããã°ãSenderããReceiverã«èªåçã«éç¥ãè¡ããã Sender* sender = [[[Sender alloc] init] autorelease]; for (int i = 0; 1000 > i; ++ i) { [sender increment]; } } }
ä¸è¨ã®ã³ã¼ãä¾ã¯æå³ã®ãªããã«ãã¹ã¬ããã§ãããããããã¬ã§è¿½ã£ã¦ã¿ãã°åããéããå®å ¨ã«éåæå¦çãè¡ããã¦ãããèªåã§ã¹ã¬ãããç¨æããããã§ããªãã®ã«ãã¤ãã³ãã®éä¿¡å ã¯ã¡ãã»ã¼ã¸ãPOSTããã¨ããã«å¾©å¸°ããã®ã ã
ããã§ã¯è©³ç´°ã¯å²æããããUIã«ãããç»é¢ã®åæç»ã¡ãã»ã¼ã¸ã®ãããªã大éã«éç¥ããã¦ãæå³ã®ãªããããªã¡ãã»ã¼ã¸ããåä¿¡å ã«POSTããåã«åé¤ããããã¯ãã¼ã¸ãã¦ãã¾ãä»çµã¿ãåãã£ã¦ããã
ãã®ããã«ãNSNotificationCenterã¯ä»ã¾ã§ç´¹ä»ãã3ã¤ã®ã¤ãã³ãä¼éã®ä»çµã¿ã®ä¸ã§ãæãé«åº¦ãªæ©è½ãæã£ã¦ãããç¹ã«ããã¯ã°ã©ã¦ã³ãã§ç¨¼åããã¹ã¬ãããããå¥ã®ã¹ã¬ããï¼ä¸»ã«ã¡ã¤ã³ã¹ã¬ããï¼ã¸ã®ã¡ãã»ã¼ã¸éç¥ã«ããã¦å¤§ããªå½¹å²ãæãããå®éã«iOSå é¨ã§ã©ã®ããã«ä½¿ããã¦ããã®ãã¨ããã¨ããµã¦ã³ãåçã®ã¤ãã³ãéç¥ããããã¤ã¹ã®åãã®å¤æ´éç¥ãªã©ããã¼ãã¦ã§ã¢å¯ãã®éåæã¤ãã³ãéç¥ã§ä½¿ç¨ããã¦ããã
éç¥ã»ã³ã¿ã¼ã«ã¯ãããã£ãé«åº¦ãªé¢ããããããã¦ããã¯ããæåã«ç´¹ä»ããé ã
ã»ãããã³ã«ããªã²ã¼ã
ã»KVO
ã»NSNotificationCenter
ã®é ã«é©ç¨ãæ¤è¨ããã®ãè¯ãã