ios - iOS中如何限制NSNotification多次调用方法?

标签 ios objective-c swift nsnotificationcenter addobserver

我正在使用 NSNotificationCenter 在我的代码中发送本地通知,并在 Objective-C 和 Swift 中工作。我正在发布来自 Objective-C 的通知并在 Swift 中接收。但是我在通知中添加的方法被多次调用,并且仅在 viewDidLoad 方法中添加了观察者。

swift :

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.serverCardSynced), name: NSNotification.Name(rawValue: NOTIF_SERVER_CARD_SYNCED), object: nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.checkForAutoSync), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.initateSync), name: NSNotification.Name(rawValue: NOTIF_CONTACT_ENTITY_CHANGE), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.menuRemoved), name: NSNotification.Name(rawValue: NOTIF_MENU_REMOVED), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.reloadAllCards(_:)), name: NSNotification.Name(rawValue: NOTIF_RELOAD_ALL_CARDS), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.initateDownloadMyCards), name: NSNotification.Name(rawValue: NOTIF_DOWNLOAD_CARD), object:nil);
}

objective-C :

- (void)applicationDidBecomeActive:(UIApplication *)application {
    self.isSyncPending = true;
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:NOTIF_CONTACT_ENTITY_CHANGE object:nil];
}

-(void)insertData(){
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:NOTIF_SERVER_CARD_SYNCED object:nil];
}

我在我的 deinit 中添加了删除观察者,但它甚至没有调用。如何停止多次调用。

最佳答案

//call this method in viewDidLoad
fileprivate func registerNotifs() {

    //remove observer before adding to make sure that it is added only once

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NOTIF_SERVER_CARD_SYNCED), object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NOTIF_CONTACT_ENTITY_CHANGE), object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NOTIF_MENU_REMOVED), object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NOTIF_RELOAD_ALL_CARDS), object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NOTIF_DOWNLOAD_CARD), object: nil)

    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.serverCardSynced), name: NSNotification.Name(rawValue: NOTIF_SERVER_CARD_SYNCED), object: nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.checkForAutoSync), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.initateSync), name: NSNotification.Name(rawValue: NOTIF_CONTACT_ENTITY_CHANGE), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.menuRemoved), name: NSNotification.Name(rawValue: NOTIF_MENU_REMOVED), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.reloadAllCards(_:)), name: NSNotification.Name(rawValue: NOTIF_RELOAD_ALL_CARDS), object:nil);
    NotificationCenter.default.addObserver(self, selector:#selector(MainScreen.initateDownloadMyCards), name: NSNotification.Name(rawValue: NOTIF_DOWNLOAD_CARD), object:nil);
}

关于ios - iOS中如何限制NSNotification多次调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49206625/

相关文章:

iOS 8 - AutoLayout 问题 - UITableViewCell 中的 UIImageView 忽略宽度限制

ios - 在 managedObjectContext 的开头插入 NSManagedObject

swift - windowShouldClose 报告未解析的标识符

ios - Spritekit 中的新场景没有显示任何内容?

objective-c - objective-c 中的实用方法

ios - 在 ios 的 opentok 上更改相机

iphone - 在 iPhone 中动态重绘调整大小的圆圈

iphone - UI 自动化 :- How to click button using javascript of a screen which is not the main screen (iPhone)?

iphone - 需要简单标签 "screensaver"帮助

ios - 当我使用 setMetadataObjectTypes : to load the type of the scanning barcode, 时,我想设置几种类型(code39、code128 等...)