我的问题是看来观察员没有像我预期的那样被删除.发布通知,并将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压入堆栈时:该方法尚未返回,因此通知为零.
使用块创建一个全局对象,例如
__block __weak ID notification;
然后,
notification = [[NSNotificationCenter defaultCenter] addobserverForname:@"fetchDIDCompleteNewData" object:nil queue:nil usingBlock:^(NSNotification *completed) { //Remove Observers [[NSNotificationCenter defaultCenter] removeObserver:notification];}];总结
以上是内存溢出为你收集整理的iOS NSNotificationCenter Observer未被删除全部内容,希望文章能够帮你解决iOS NSNotificationCenter Observer未被删除所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)