如果您想检测您的应用是否与Firebase数据库后端有连接,您可以收听/.info/connected
.Firebase文档中有关检测连接状态的示例应该可以解决问题:
let connectedRef = FIRDatabase.database().referenceWithPath(".info/connected") connectedRef.observeEventType(.Value, withBlock: { snapshot in if let connected = snapshot.value as? Bool where connected { print("Connected") } else { print("Not connected") } })
Swift 3.1
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") } })
目标C.
FIRDatabaseReference *connectedRef = [[FIRDatabase database] referenceWithPath:@".info/connected"]; [connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { if([snapshot.value boolValue]) { NSLog(@"connected"); } else { NSLog(@"not connected"); } }];