收到Push崩溃时发布NSNotification

时间:2016-04-02 19:08:15

标签: ios swift apple-push-notifications nsnotificationcenter appdelegate

当我收到推送通知时,我通过NSNotificationCenter发布了NONotificaionName(来自下面的AppDelegate方法):

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

  playSoundWithUserInfo(userInfo)
  NSNotificationCenter.defaultCenter().postNotificationName(kNotifPushReceived, object: nil)

}

我正在让我的一个视图控制器成为此通知的观察者:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(retrieveUsersAndSetData(_:)), name: kNotifPushReceived, object: nil
}

func retrieveUsersAndSetData(completed : (() -> Void)?) {
    Friendship.retrieveFriendshipsForUser(backendless.userService.currentUser, includeGroups: false) { (friendships, fault) -> Void in
    guard let friendships = friendships else { return }
    self.friendships = friendships
    self.tableView.reloadData()
  }
}

大多数时候我收到推送通知,我在appDelegate收到崩溃,其中调用了postNotificationName():

enter image description here

崩溃说:

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x10070)

现在我读过的大多数答案似乎与这个问题相似,表明作为观察者的对象从未被正确释放,但我不相信这是因为这是目前唯一的观点 - 应用程序中的控制器,我正在从作为通知观察者的视图控制器实现以下内容:

  deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
  }

总而言之,我无法弄清楚为什么当我在app委托中收到推送通知然后发布我的NSNotification时,这种崩溃的大部分时间都会发生。

1 个答案:

答案 0 :(得分:0)

处理程序方法' fetchCompletionHandler'期望您在处理结束时调用completionHandler:

 completionHandler(UIBackgroundFetchResult.NewData)

请阅读此方法的文档,还有更多要考虑的内容。