簡體   English   中英

iOS NSNotificationCenter Observer未被刪除

[英]iOS NSNotificationCenter Observer not being removed

我在AppDelegate中有以下代碼。 目的是創建幾個觀察者,然后調用一些代碼。 一旦該代碼完成,然后發布通知,然后觀察者應該刪除兩個觀察者並調用完成處理程序。

我的問題是看來觀察員沒有像我預期的那樣被刪除。 發布通知,並將NSLog條目寫入控制台,因此我知道觀察者正在工作。 但是,在第二次調用時,NSLog被調用兩次,第三次調用三次等。

我的想法是,這與從觀察者運行的代碼塊中的刪除有關,但是,我不確定如何解決這個問題(如果這確實是問題所在)。

有人可以如此善良地解釋我是如何實現這一目標的嗎?

謝謝。

-(void) application:(UIApplication *)application performFetchWithCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {

    [[NSNotificationCenter defaultCenter] addObserverForName:@"fetchDidCompleteNewData" object:nil
                                                     queue:nil usingBlock:^(NSNotification *completed) {

                                                         //Remove Observers
                                                         [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                                         name:@"fetchDidCompleteNewData"
                                                                                                       object:nil];
                                                         [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                                         name:@"fetchDidCompleteNoData"
                                                                                                       object:nil];


                                                         // Post completion
                                                         completionHandler(UIBackgroundFetchResultNewData);
                                                         NSLog(@"Background fetch completed... New Data");
                                                     }];

    [[NSNotificationCenter defaultCenter] addObserverForName:@"fetchDidCompleteNoData" object:nil
                                                       queue:nil usingBlock:^(NSNotification *completed) {

                                                           //Remove Observers
                                                           [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                                           name:@"fetchDidCompleteNoData"
                                                                                                         object:nil];

                                                           [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                                           name:@"fetchDidCompleteNewData"
                                                                                                         object:nil];

                                                           //post completion
                                                           completionHandler(UIBackgroundFetchResultNoData);
                                                           NSLog(@"Background fetch completed... No New Data");

                                                       }];

    GetDetails *getDetails = [[GetDetails alloc] init];
    [getDetails backgroundRefresh];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    return YES;
}

您沒有將self注冊為對象。 此外,當塊被addObserverForName:壓入堆棧時addObserverForName:該方法尚未返回,因此notification nil

使用block一個全局對象,例如

__block __weak id notification;

然后,

notification =  [[NSNotificationCenter defaultCenter] addObserverForName:@"fetchDidCompleteNewData" object:nil queue:nil usingBlock:^(NSNotification *completed) {

   //Remove Observers
   [[NSNotificationCenter defaultCenter] removeObserver:notification];
}];

我的想法是,這與從觀察者運行的代碼塊中的刪除有關,但是,我不確定如何解決這個問題(如果這確實是問題所在)。

有人可以如此善良地解釋我是如何實現這一目標的嗎?

當然。

您可以通過不使用addObserverForName:object:queue:usingBlock:方法而使用addObserver:selector:name:object:方法輕松測試您的理論,其中selector是您調用的函數的名稱而不是使用塊。

只需使用NSNotificationCenterAPI指南來獲取有關這些方法的詳細信息,或者一般情況下,因為您質疑的是您可以使用哪種不需要塊語句的方法,請咨詢API是第一個檢查其中的替代工具的方法。類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM