1
votes

I am developing an app in swift and using Firestore to store my data. It’s the first time that I am using Firebase.

Yesterday I connected the firebase and firestore to my project and everything worked fine. Today suddenly I got an error that It could not connect to the Firestore backend.

2020-04-03 13:37:25.851931+0200 Bouwresten[14277:2448929] 6.21.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 time. Most recent error: Network connectivity changed This typically indicates that your device does not have a healthy Internet connection at the moment. 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. Does anyone know what the problem could be?

This is my 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

//
//  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.

6
How have you installed Firebase / Firestore? If it's through CocoaPods, it might actually be an issue with this. I would try uninstalling and reinstalling the Podfile. Whenever I have a Firebase issue, it's usually because of this, even though Xcode will often claim it's something else. I hope this helps!Kasey
I installed it via the Podfile. I tried to reinstall it but I got the same problemBrecht Verhelst
Have you found the solution to your issue? Although I don't face the problem off, my client said that they had.Thanh Vũ Trần

6 Answers

1
votes

Your code seems ok. I have the same problem with the XCode simulator since a while, but on a real device everything works fine. So I assume it is an XCode issue/bug which can be ignored.

1
votes

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!

  1. Change your Firebase version in your package.json to: "firebase": "7.14.6
  2. Then add the base-64 package: https://www.npmjs.com/package/base-64
  3. Add this code snippet to your 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

  2. Try again and it should work!

1
votes

I solved this error by syncing my computer and device times. They were off by a few seconds (roughly 15).

For that, go to settings -> synchronize your clock -> sync now (windows 10).

In my case, firestore only couldn't be reached when the debugger was on. You might want to check that. Working on a React Native project here.

0
votes

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?

Side note: You only need to import Firebase for all files that use any part of Firebase.

0
votes

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

0
votes

I solved this by deleting my pod file and deleting my firebase connection from my app. Reinstalled it and everything worked perfect!