处理 launchOptions 时的 RxSwift FirebaseDatabase 快照 <null>?[UIApplicationLaunchOptionsKey.remoteNotification]

RxSwift FirebaseDatabase snapshot <null> when handling launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]

Firebase 上存在对象。根据对象 ID 打开详细视图。 当应用程序处于后台并且从

调用该函数时,它工作正常
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void)

当应用程序被终止并且通知已处理时,快照返回 null。

应用程序代表:

AppName.shared.user.shareReplayLatestWhileConnected().filterNil().distinctUntilChanged({ lhs, rhs in
        return lhs.key == rhs.key
    }).subscribe() { event in
        switch event {
        case .next(let user):
            self.userIdString = user.key
           ...
            if let notificationInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
                self.handleMessageFromRemoteNotification(notificationInfo)
                }
            }...

处理程序:

Event.load(url.lastPathComponent).single().subscribe(onNext: { event in
            //prepare and present view here...
        }).addDisposableTo(disposeBag)

事件加载函数失败:

static func load(_ id: String) -> Observable<Event> {
    return Observable.create() { observer in
        FirebaseDatabase.sharedInstance.ref.child("events/\(id)").observeSingleEvent(of: .value, with: {
            snapshot in
            //This the null snapshot
            if snapshot.exists() {
                if let event = Event(snapshot: snapshot) {
                    observer.onNext(event)
                }
                observer.on(.completed)
            } else {
                observer.on(.completed)
            }
        })
        return Disposables.create()
    }
}

暂时

FirebaseDatabase.sharedInstance.ref.child("events/\(id)").observe(.value

相对于

FirebaseDatabase.sharedInstance.ref.child("events/\(id)").observeSingleEvent(of: .value

好像解决了,会多多评论。