iOS 登录令牌过期后 Firebase 同步无法正常工作

iOS Firebase sync not working after login token expires

Firebase iOS 版本:2.4.1.1

登录令牌过期后,从移动设备到 firebase 的上游同步停止。但是下游同步仍在工作。

这不是我所期望的,因为我不要求用户登录才能写入 Firebase。

这通常不明显,但当我尝试使用非常短的会话长度时,它变得很明显。在我的测试中,我使用了 24 秒,通过管理界面设置。

我对默认行为有奇怪的期望还是我无意中配置了什么?

Firebase 规则

{
"rules": {
    ".read": true,
    ".write": true
    }
}

在我的 AppDelegate 中

override init() {

    super.init()
    Firebase.defaultConfig().persistenceEnabled = true
    let ref = Firebase(url: "https://xxx.firebaseio.com");
    ref.authUser("xxx@gmail.com", password: "1234") {
        error, authData in
        if error != nil {
            // an error occured while attempting login
            print(error)
        } else {
            print("login sucessfully experiy date \(NSDate(timeIntervalSince1970: authData.expires.doubleValue))")
        }
    }

    ref.observeAuthEventWithBlock({ authData in
        if authData != nil {
            // user authenticated
            print("AuthEvent \(authData)")
        } else {
            // No user is signed in
            print("AuthEvent: user is signed out")
        }
    })
}

我有以下上传代码(示例)

func createSubscription(subscription: Subscription) {
    let commentRef = fireSubs.childByAutoId()
    commentRef.setValue(subscription.fireDict())
}

下游(示例)

fireSubs.observeEventType(.ChildAdded, withBlock: { snapshot in
    self.subscriptions.insert(Subscription(snapshot: snapshot), atIndex: 0)
    self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Top)
})

显然这与启用的离线功能有关: https://www.firebase.com/docs/ios/guide/offline-capabilities.html

If our app uses Firebase Authentication, the client will persist the user's authentication token across restarts. If the auth token expires while our app is offline, the client will pause our write operations until we re-authenticate, else our writes might fail due to security rules

因此该应用在某种程度上确实遵循了记录的行为。一件事是暂停写入不仅会在离线时发生,还会在在线且令牌已过期时发生。

关闭磁盘持久性并且写入不会暂停。