【问题标题】:How to detect if user is online when using Firebase iOS SDK使用 Firebase iOS SDK 时如何检测用户是否在线
【发布时间】:2014-05-23 10:04:59
【问题描述】:

在发送消息(即在 Firebase 对象上调用 setValue)之前,是否有推荐的方法来确定用户是在线还是离线?

例如:

[firebase setValue:someValue withCompletionBlock:^(NSError *error, Firebase *ref) {

    // This block is ONLY executed if the write actually completes. But what if the user was offline when this was attempted?
    // It would be nicer if the block is *always* executed, and error tells us if the write failed due to network issues.

}];

我们需要在我们的 iOS 应用程序中使用它,因为如果用户进入隧道,他们可能会失去连接。如果 Firebase 不提供内置方法来执行此操作,我们将仅求助于监控 iOS 的 Reachability API。

【问题讨论】:

    标签: ios firebase


    【解决方案1】:

    斯威夫特 3


    let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
    connectedRef.observe(.value, with: { snapshot in
        if let connected = snapshot.value as? Bool, connected {
            print("Connected")
        } else {
            print("Not connected")
        }
    })
    

    更多信息 - https://firebase.google.com/docs/database/ios/offline-capabilities

    【讨论】:

      【解决方案2】:

      你可以做这样的事情。设置观察者并发布状态更改通知。与接受的答案基本相同,但适应了新版本的 firebase 框架。

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          ...
          FIRDatabaseReference *ref = [[FIRDatabase database] referenceWithPath:@".info/connected"];
          [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
                  NSString *value = snapshot.value;
                  NSLog(@"Firebase connectivity status: %@", value);
                  self.firebaseConnected = value.boolValue;
      
                  [[NSNotificationCenter defaultCenter] postNotificationName:@".fireBaseConnectionStatus" object:nil];
          }];
      }
      

      然后在您应用的任何视图控制器中,您都可以执行此操作。观察通知并根据通知做一些事情(更新你的 ui 等)。

      - (void) fireBaseConnectionStatus:(NSNotification *)note
      {
              AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
              [self updateButtons:app.firebaseConnected];
      }
      
      - (void)viewDidLoad
      {
              [super viewDidLoad];
              [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fireBaseConnectionStatus:) name:@".fireBaseConnectionStatus" object:nil];
      }
      

      希望这会有所帮助。

      PS。也许您会发现使用众所周知的可达性来监控基本可达性是一个有趣的想法。[mh] 框架。然后,您还可以决定在 firebase 连接到 wifi 或 3g 时如何操作。

      【讨论】:

        【解决方案3】:

        他们的文档中有一部分专门用于此here.

        基本观察.info/connected ref

        Firebase* connectedRef = [[Firebase alloc] initWithUrl:@"https://SampleChat.firebaseIO-demo.com/.info/connected"];
        [connectedRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot, NSString *prevName) {
            if([snapshot.value boolValue]) {
                // connection established (or I've reconnected after a loss of connection)
            }
            else {
                // disconnected
            }
        }];
        

        【讨论】:

          猜你喜欢
          • 2017-11-20
          • 2016-10-18
          • 1970-01-01
          • 2017-05-03
          • 2012-04-26
          • 2018-10-28
          • 1970-01-01
          • 2019-02-15
          相关资源
          最近更新 更多