繁体   English   中英

如何使用Swift3检测与Firebase数据库的互联网连接?

[英]How to detect internet connection with Firebase Database using Swift3?

任何人都可以帮助我使用Swift 3检测与Firebase数据库的互联网连接吗? 我正在使用此功能从数据库下载数据。

  func loadData(){


    Ref=FIRDatabase.database().reference()

    Handle = Ref?.child("Posts").queryOrdered(byChild: "Des").queryEqual(toValue: "11").observe(.childAdded ,with: { (snapshot) in

        if  let post = snapshot.value as? [String : AnyObject] {

            let img = Posts()

            img.setValuesForKeys(post)

            self.myarray.append(img)

                self.tableView.reloadData()

        }else {


        }

    })

}

如果要检测您的应用是否与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");
  }
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM