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

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

问题描述

任何人都可以帮助我使用Swift 3检测与Firebase数据库的互联网连接?我使用这个函数从数据库下载数据。
$ b $ pre $ func loadData(){


Ref = FIRDatabase.database()。reference()

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

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

let img = Posts()

img.setValuesForKeys(post)

self.myarray.append(img)

self.tableView.reloadData()

} else {


}

})

}


解决方案

如果您想要检测您的应用是否与Firebase数据库后端有连接,您可以侦听 /。方式/连接。来自关于检测连接状态的Firebase文档< a>应该这样做:

  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(未连接)
}
})

Swift 3.1

  let connectedRef = FIRDatabase.database()。reference(withPath:.info /连接)
connectedRef.observe(.value,与:{快照在
如果让connected = snapshot.value为?Bool,连接{
print(连接)
} else {
print(Not connected)
}
})

目标C

  FIRDatabaseReference * connectedRef = [[FIRDatabase database] referenceWithPath : 方式/连接 @]; 
[connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * snapshot){
if([snapshot.value boolValue]){
NSLog(@connected);
} else {
NSLog(@not connected);
}
}];


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 {


        }

    })

}

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

Objective 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天全站免登陆