NSNotification pas envoyé lorsque postNotificationName: appelé
-
22-09-2019 - |
Question
Je suis en train d'en obtenir un exemple d'utiliser NSNotificationCenter
avec addObserver
et postNotificationName
mais je ne peux pas comprendre pourquoi il ne fonctionnera pas.
J'ai 2 lignes de code pour ajouter l'observateur et d'envoyer le message dans 2 classes différentes
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded:) name:@"Event" object:nil];
et
[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
Si je mets le nom nil
il fonctionne très bien, il est juste becuase une émission, quand je tente de définir un nom de notification, les messages ne sont jamais à travers.
La solution
Tout mon code utilise NSNotifications
comme ceci:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ScanCompleted" object:nil];
La première enregistre la notification et la deuxième publication de la notification.
Autres conseils
Fondamentalement, il est tout à voir avec l'ordre d'exécution. Si vous avez exécuté postNotificationName avant addObserver, cela est un problème facile à avoir. Utiliser les points d'arrêt et de parcourir le code:)
Votre premier point d'arrêt devrait arrêter ici:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"ScanCompleted" object:nil];
Alors ici:
[[NSNotificationCenter defaultCenter]postNotificationName:@"ScanCompleted" object:self];
Aussi, assurez-vous que le sélecteur a deux points sur. Parce qu'il est la signature de la méthode sera:
- (void)updateView:(NSNotification *)notification;
J'ai eu le même problème. La raison en est que j'ai appelé méthode removeObserver à
- (void)viewDidDisappear:(BOOL)animated{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
vérifier Alors, que si vous aviez appelé removeObserver avant postNotification.
Conseils. Vous pouvez rechercher le mot-clé « removeObserver » à trouver si vous aviez appelé cette fonction
Changer ceci:
[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
à ceci:
[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:nil];
Si votre première notification est enregistrée correctement, newEventLoaded doit être appelé.
J'ai eu un problème similaire et mon problème était dû à la notification d'être appelé sur un autre fil. Cela a résolu mon problème.
dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
});
Avez-vous essayé d'autres noms, mais @ « événement » et nul? Juste pour être sûr, vous pouvez définir vos noms d'événement dans un fichier et l'inclure à la fois l'enregistrement de notification et l'envoi. Par exemple:
fichier d'en-tête:
extern NSString * const NOTE_myEventName;
Fichier source:
NSString * const NOTE_myEventName = @"MyEventName";
Inscription:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleMyEvent:)
name:NOTE_myEventName
object:nil];
Notification envoi:
[[NSNotificationCenter defaultCenter]
postNotificationName:NOTE_myEventName object:nil];
Je fixe mon succès "NSNotification
pas envoyé lorsque postNotificationName:
appelé" crash.
J'ai trouvé le vrai bug est gestionnaire de messages de notification.
Le postNotificationName
et addObserver
sont bien comme le premier poste de ce fil.