特定のNSNotificationを監視するクラスがいくつかある場合、通知がポストされたときにオブザーバーに通知される順序は?オブザーバー通知のNSNotification命令
18
A
答えて
18
どのような注文通知が送信されるかに関する保証はありません。注文が必要な場合は、1つの通知を待ち受けるクラスを作成し、他のクラスが代わりに聞くことができる複数の注文通知を送信することができます。
5
注文は未定義です。 Appleはオブザーバーのリストを管理し、通知が掲示されるたびにリストを反復し、登録されたすべてのオブザーバーに通知します。リストは配列や辞書、あるいは完全に異なるもの(例えば構造体のリンクリスト)であってもよいし、いつでも実行時にオブザーバを追加したり削除したりできるので、いつでもリストが変更される可能性がある。リストは現在実装されているので、決して特定の注文に頼ることはできません。さらに、OS Xのアップデートによって、リストの内部構造が変更される可能性があり、10.7に当てはまるものが10.8または10.6に当てはまらないことがあります。
0
私はそれをテストしていると、オブジェクトがこのテストのためのaddObserver方法
コンソル出力があるが発注されているように見える:
#import "AppDelegate.h"
#import "ViewController.h"
#include <stdlib.h>
@interface AppDelegate()
@property (strong, readwrite, nonatomic) NSTimer *timer;
@property (strong, readwrite, nonatomic) NSMutableArray *array;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.array = [NSMutableArray array];
ViewController *vc3 = [ViewController new]; vc3.index = 8;
ViewController *vc1 = [ViewController new]; vc1.index = 1;
ViewController *vc2 = [ViewController new]; vc2.index = 2;
[self.array addObject:vc1];
[self.array addObject:vc3];
[self.array addObject:vc2];
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(sendNotification:) userInfo:nil repeats:YES];
return YES;
}
- (void)sendNotification:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTitle1 object:nil];
}
@end
AppDelegate.m
2016-04-04 22:04:02.627 notificationsTest[1910:763733] controller 8
2016-04-04 22:04:02.629 notificationsTest[1910:763733] controller 1
2016-04-04 22:04:02.629 notificationsTest[1910:763733] controller 2
ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController()
@property (assign, readwrite, nonatomic) NSInteger index;
@end
@implementation ViewController
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondToNotification:) name:kNotificationTitle1 object:nil];
}
return self;
}
- (void)respondToNotification:(NSNotification *)notification {
NSLog(@"controller %ld", self.index);
}
@end
関連する問題
- 1. NSNotificationオブザーバーとしてのクラス?
- 2. マージ2人の通知のオブザーバーはRxSwift
- 3. 命令レベルプロファイリング:命令ポインタの意味?
- 4. 「オブザーバー」と「通知」(プロパティブロック内)との違い「オブザーバー」と「リスナー」の比較ポリマー
- 5. デコード68k命令
- 6. ドッカーファイルONBUILD命令
- 7. LEA命令?
- 8. MIPSシフト命令
- 9. ストリング命令
- 10. MIPSの 'align'命令
- 11. ARMのプリフェッチ命令
- 12. SIGILL(BL命令の)
- 13. デコード命令のパターン
- 14. スカラのバイトコードで不要なロード命令とストア命令
- 15. 同じアセンブリ命令が、別のマシン命令
- 16. エミュレータの開発で命令セットを知るには?
- 17. ビルドプロセスが失敗しました:未知の命令:ARG
- 18. WCF命令的バインド
- 19. アームサムモード4バイト命令
- 20. ASP.Net実行命令
- 21. 命令ポインタとプログラムカウンタ?
- 22. アセンブリMIPS TAl命令
- 23. MIPS分岐命令
- 24. SSE命令:Byte + Short
- 25. アセンブリJZ命令CMP
- 26. アセンブラSTRB命令は
- 27. WHERE NOT SQL命令
- 28. MIPSの命令の質問
- 29. PLD命令の使用
- 30. ジャンプ命令のMIPS範囲
私は、あなたにオブザーバーを追加するのと同じ順序で通知されると思います。とにかく、これはNSNotificationCenterの内部についてのものであり、将来変更される可能性があるので、私はこの事実に頼っていません。 – atxe