简体   繁体   English

连接到 firebase/firestore 时出错:无法到达 Cloud Firestore 后端

[英]Error when connecting to firebase/firestore: Could not reach Cloud Firestore backend

I am developing an app in swift and using Firestore to store my data.我正在 swift 开发一个应用程序并使用 Firestore 来存储我的数据。 It's the first time that I am using Firebase.这是我第一次使用 Firebase。

Yesterday I connected the firebase and firestore to my project and everything worked fine.昨天我将firebasefirestore连接到我的项目,一切正常。 Today suddenly I got an error that It could not connect to the Firestore backend.今天突然报错说无法连接到Firestore后端。

2020-04-03 13:37:25.851931+0200 Bouwresten[14277:2448929] 6.21.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. 2020-04-03 13:37:25.851931+0200 Bouwresten[14277:2448929] 6.21.0 - [Firebase/Firestore][I-FST000001] 无法到达 Cloud Firestore 后端。 Connection failed 1 time.连接失败 1 次。 Most recent error: Network connectivity changed This typically indicates that your device does not have a healthy Inte.net connection at the moment.最近的错误:网络连接已更改这通常表示您的设备目前没有健康的 Inte.net 连接。 The client will operate in offline mode until it is able to successfully connect to the backend.客户端将以离线模式运行,直到它能够成功连接到后端。

Now I know for sure that my computer has a good connection and I am using the simulator of Xcode to test the app.现在我确定我的电脑连接良好,我正在使用 Xcode 的模拟器来测试应用程序。 Does anyone know what the problem could be?有谁知道是什么问题?

This is my appdelegate.swift这是我的 appdelegate.swift

//
//  AppDelegate.swift
//  Bouwresten
//
//  Created by Brecht Verhelst on 25/03/2020.
//  Copyright © 2020 Brecht Verhelst. All rights reserved.
//

import UIKit
import Firebase
import FirebaseFirestore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

This is the class where I use the firestore to get data, it's called dbhelper这是我使用 firestore 获取数据的 class,它叫做 dbhelper

//
//  DBHelper.swift
//  Bouwresten
//
//  Created by Brecht Verhelst on 02/04/2020.
//  Copyright © 2020 Brecht Verhelst. All rights reserved.
//

import Foundation
import Firebase
import FirebaseFirestore

class DBHelper
{
    var Users = [User]()
    var Products = [Product]()
    init() {
        getUsers()
    }

    func getUsers()  {
        Firestore.firestore().collection("Gebruikers").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let user:User = User(Voornaam: document.get("Naam") as! String, Familienaam: document.get("Familienaam") as! String, Email: document.get("Email") as! String, Wachtwoord: document.get("Wachtwoord") as! String,
                                         Stad: document.get("Stad") as! String, Postcode: document.get("Postcode") as! String, Straat: document.get("Straat") as! String, Nummer: document.get("Nummer") as! String, Added_on: document.get("Added_on") as! String)
                    self.Users.append(user)
                    print(self.Users.count)
                }
            }
        }
    }

    func getProducts()  {
        Firestore.firestore().collection("Producten").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let product:Product = Product(Titel: document.get("Titel") as! String, Beschrijving: document.get("Beschrijving") as! String, Prijs: document.get("Prijs") as! Double, Gereserveerd: document.get("Gereserveerd") as! Bool, Added_on: document.get("Added_on") as! String, Gebruiker_id: document.get("gebruiker_ID") as! String)
                    self.Products.append(product)
                }
            }
        }
        print(self.Products.count)
    }
}

Hope someone can help me with this problem.希望有人能帮我解决这个问题。

Your code seems ok.你的代码似乎没问题。 I have the same problem with the XCode simulator since a while, but on a real device everything works fine.一段时间以来,我对 XCode 模拟器有同样的问题,但在真实设备上一切正常。 So I assume it is an XCode issue/bug which can be ignored.所以我认为这是一个可以忽略的 XCode 问题/错误。

It's probably a bug in the most recent version of Firebase (7.14.6), I tried switching to an older version and it worked for me!这可能是最新版本的 Firebase (7.14.6) 中的一个错误,我尝试切换到旧版本,它对我有用!

  1. Change your Firebase version in your package.json to: "firebase": "7.14.6将 package.json 中的 Firebase 版本更改为:“firebase”:“7.14.6
  2. Then add the base-64 package: https://www.npmjs.com/package/base-64然后添加base-64 package: https://www.npmjs.com/package/base-64
  3. Add this code snippet to your App.js:将此代码段添加到您的 App.js:

 import {decode, encode} from'base-64'; if (.global.btoa) { global;btoa = encode. } if (.global;atob) { global.atob = decode; }

  1. Run npm install in your root folder of your project在项目的根文件夹中运行 npm install

  2. Try again and it should work!再试一次,它应该工作!

I solved this error by syncing my computer and device times.我通过同步我的计算机和设备时间解决了这个错误。 They were off by a few seconds (roughly 15).他们离开了几秒钟(大约 15 秒钟)。

For that, go to settings -> synchronize your clock -> sync now (windows 10).为此,go 设置 -> 同步您的时钟 -> 立即同步(Windows 10)。

In my case, firestore only couldn't be reached when the debugger was on.就我而言,只有在调试器打开时才能访问 firestore。 You might want to check that.你可能想检查一下。 Working on a React Native project here.在此处处理 React Native 项目。

There doesn't look anything wrong with your code.您的代码看起来没有任何问题。

Have you made absolutely sure the device (of Mac/Macbook if you're running on a simulator) has a good internet connection as per the error message?根据错误消息,您是否完全确定设备(如果您在模拟器上运行,则为 Mac/Macbook)具有良好的互联网连接?

Side note: You only need to import Firebase for all files that use any part of Firebase.旁注:您只需为使用 Firebase 任何部分的所有文件import Firebase Firebase。

Please update your rules with请更新您的规则

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read:  if request.auth != null;
      allow write: if false;
    }
  }
}

See details here https://github.com/firebase/firebase-ios-sdk/issues/3763在此处查看详细信息https://github.com/firebase/firebase-ios-sdk/issues/3763

I solved this by deleting my pod file and deleting my firebase connection from my app.我通过删除我的 pod 文件并从我的应用程序中删除我的 firebase 连接解决了这个问题。 Reinstalled it and everything worked perfect!重新安装它,一切都很完美!

I just had this issue and solved it simply by...我刚刚遇到这个问题并通过...简单地解决了它

allow write

In the Rules在规则中

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

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