如何使用 Swift3 检测与 Firebase 数据库的互联网连接? [英] How to detect internet connection with Firebase Database using Swift3?

查看:17
本文介绍了如何使用 Swift3 检测与 Firebase 数据库的互联网连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can any one help me to detect internet connection with Firebase Database using Swift 3? I am using this function to download data from database.

  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 文档 应该可以解决问题:

If you want to detect whether your app has a connection to the Firebase Database backend, you can listen for /.info/connected. This example from the Firebase documentation on detecting connection state should do the trick:

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");
  }
}];

这篇关于如何使用 Swift3 检测与 Firebase 数据库的互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆