2014-04-09 12 views
5

Trước khi gửi thư (ví dụ: gọi setValue trên đối tượng Firebase), có cách nào được đề xuất để xác định xem người dùng có trực tuyến hay ngoại tuyến không?Cách phát hiện xem người dùng có trực tuyến hay không khi sử dụng SDK Firebase iOS

Ví dụ:

[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. 

}]; 

Chúng tôi cần điều này trong ứng dụng iOS của chúng tôi bởi vì người dùng có thể mất kết nối nếu họ đi vào một đường hầm ví dụ. Nếu Firebase không cung cấp cách tích hợp để thực hiện việc này, chúng tôi sẽ chỉ sử dụng để theo dõi API khả năng hiển thị của iOS.

Trả lời

5

Họ có một phần của tài liệu của họ dành cho việc này here.

Về cơ bản tuân thủ các .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 
    } 
}]; 
1

Bạn có thể làm một cái gì đó như thế này. Thiết lập người quan sát và đăng thông báo về thay đổi trạng thái. Về cơ bản giống như câu trả lời được chấp nhận nhưng thích nghi với phiên bản mới của khung công tác 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]; 
    }]; 
} 

Sau đó, trong mọi trình điều khiển chế độ xem của ứng dụng, bạn có thể thực hiện việc này. Quan sát các thông báo và làm một cái gì đó dựa trên đó (cập nhật ui của bạn, vv).

- (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]; 
} 

Hy vọng điều này sẽ hữu ích.

PS. Có lẽ bạn sẽ tìm thấy nó ý tưởng thú vị để theo dõi khả năng tiếp cận cơ bản với khả năng tiếp cận nổi tiếng. [Mh] framework. Sau đó, bạn cũng có thể quyết định cách bạn hành động trong trường hợp firebase được kết nối trên wifi hoặc 3g.

Các vấn đề liên quan