特定の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. スイフト3:NSNotificationと通知
- 3. ビルドイメージ - 未知の命令
- 4. 通知ビルダの命令が機能しない
- 5. マージ2人の通知のオブザーバーはRxSwift
- 6. RDTSCPと命令命令
- 7. 命令レベルプロファイリング:命令ポインタの意味?
- 8. 「オブザーバー」と「通知」(プロパティブロック内)との違い「オブザーバー」と「リスナー」の比較ポリマー
- 9. Android通知とバッテリー寿命
- 10. NSNotification NSMetadataQueryDidUpdateノンストップ通知メッセージを取得する
- 11. ストリング命令
- 12. 命令コード
- 13. NASM命令シーケンス
- 14. ハイパージッガーノード - レッドクエリ命令
- 15. MIPSシフト命令
- 16. デコード68k命令
- 17. ドッカーファイルONBUILD命令
- 18. Graphhopperディスエーブル命令
- 19. LEA命令?
- 20. デコード命令のパターン
- 21. ARMのプリフェッチ命令
- 22. MIPSの 'align'命令
- 23. SIGILL(BL命令の)
- 24. Cortex M7のNEON命令とSIMD命令の正確な違い
- 25. 自動プロパティの初期化IL命令の命令
- 26. "out"と "in"命令の特権命令はありますか?
- 27. スカラのバイトコードで不要なロード命令とストア命令
- 28. 同じアセンブリ命令が、別のマシン命令
- 29. ビルドプロセスが失敗しました:未知の命令:ARG
- 30. エミュレータの開発で命令セットを知るには?
私は、あなたにオブザーバーを追加するのと同じ順序で通知されると思います。とにかく、これはNSNotificationCenterの内部についてのものであり、将来変更される可能性があるので、私はこの事実に頼っていません。 – atxe