firebase-ios-sdk: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
[REQUIRED] Step 1: Describe your environment
- Xcode version: 12.4
- Firebase SDK version: 7.7.0
- Installation method:
CocoaPods - Firebase Component: Firestore
[REQUIRED] Step 2: Describe the problem
Steps to reproduce:
Open app Wait for error message to appear in debugger
Relevant Code:
final class CandidateData: ObservableObject {
@Published var candidates: [Candidate] = fetchCandidateData()
}
func fetchCandidateData() -> [Candidate] {
let db = Firestore.firestore()
var candidates: [Candidate]?
let semaphore = DispatchSemaphore(value: 0)
db.collection("candidate-profiles").addSnapshotListener() { querySnapshot, err in
defer { semaphore.signal() }
if let err = err {
print("Failed to get candidates: \n \(err)")
} else {
guard let documents = querySnapshot?.documents else {
print("Failed to get candidates: \n No documents")
return
}
candidates = documents.map{ queryDocumentSnapshot -> Candidate in
let data = queryDocumentSnapshot.data()
var name = "",age = "",occupation = "",affiliation = "",bio = "",logo = "" // Stats
var questions = ["":""]
var images = [""]
var blurb = [""]
if let statData = data["stats"] as? [String: Any] {
name = statData["name"] as? String ?? ""
age = statData["age"] as? String ?? ""
occupation = statData["occupation"] as? String ?? ""
affiliation = statData["affiliation"] as? String ?? ""
bio = statData["bio"] as? String ?? ""
logo = statData["campaign-logo-url"] as? String ?? ""
}
if let questionData = data["questions"] as? [String:Any] {
questions = questionData as? [String:String] ?? ["":""]
}
if let imageData = data["images"] as? [String] {
images = imageData
}
if let blurbData = data["blurb"] as? [String] {
blurb = blurbData
}
return Candidate(id: queryDocumentSnapshot.documentID, name: name, age: Int(age)!, logo: logo, blurb: blurb, images: images, occupation: occupation, affiliation: affiliation, bio: bio, questions: questions)
}
}
}
semaphore.wait()
return candidates!
}
Leads to the error:
2021-02-25 22:24:32.209251-0800 Politifi[2812:790636] 7.3.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
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.
I’m able to make reads and writes on Safari on the iPhone 8 Plus running iOS 14.4 using the Firebase Console, so connectivity is not the issue. For posterity I tried both WiFi and 4G LTE, but there was no difference made.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21 (11 by maintainers)
Man, I feel like a dunce. Removing that fixed everything. Thanks for all your help! 🥳 🥳
Update: I’ve confirmed that blocking the main thread is the root cause of this issue. Removing the call to
semaphore.wait()(and tweaking the code so that it compiles) fixes theCould not reach Cloud Firestore backenderror. I’ll look into improving the error message to report this scenario more clearly, but you will need to find a non-blocking way to “wait” for the results to come back from Firestore.