Currently I’ve set firebase for android and iOS in a KMM project.
I’ve added firebase sdk on both projects for the native part to configure them.
On the shared side, I’m using gitlive firebase sdk
library, where I do a query to firestore database. So the logic to query data is SHARED
Here’s the query:
@OptIn(ExperimentalCoroutinesApi::class)
override fun invoke(): Flow<List<TicketUi>> = flow {
val list = Firebase.firestore
.collection("users")
.document(Firebase.auth.currentUser!!.uid)
.collection("receipts")
.snapshots
.mapLatest {
it.documents.map { doc ->
// Additional code converting the document to a Receipt
}
}
// Collect the flow and emit the list
list.collect { ticketUiList ->
emit(ticketUiList)
}
}.catch { error ->
Logger.e { "Error in GetAllTicketsUseCaseImpl.invoke(): ${error.cause} && message: ${error.message}" }
emit(emptyList<TicketUi>())
}
I’ve set up the emulator in order to test it on both platforms.
Android
It works perfectly, all good
iOS:
- I’ve created a iOS project on Firebase
- GoogleServices-Info.plist has been added to the project
- I’ve added Firebase SDK to the project, that includes
Auth
&Firestore
. - On
AppDelegate
I call the following code
FirebaseApp.configure()
// Firestore Configuration
let settings = Firestore.firestore().settings
settings.host = "127.0.0.1:8080"
settings.cacheSettings = MemoryCacheSettings()
settings.isSSLEnabled = false
Firestore.firestore().settings = settings
FirebaseConfiguration.shared.setLoggerLevel(.debug)
Auth.auth().useEmulator(withHost:"127.0.0.1", port:8081)
let auth = Auth.auth()
if auth.currentUser == nil {
// Sign in anonymously
auth.signInAnonymously { authResult, error in
if let error = error {
// Sign-in failed∂
print("signInAnonymously:failure (error.localizedDescription)")
} else {
// Sign-in succeeded
print("signInAnonymously:success")
if let user = authResult?.user {
print("User: (user.uid)")
}
}
}
} else {
// Already signed in
print("Already logged in: (auth.currentUser?.uid ?? "No UID")")
}
Cool. Time to query the data from firestore for that specific user. The following error shows up:
Error Domain=FIRFirestoreErrorDomain Code=7 “Missing or insufficient
permissions.” UserInfo={NSLocalizedDescription=Missing or insufficient
permissions.
11.1.0 – [FirebaseFirestore][I-FST000001] Listen for query at users/9hYte9W9Sf1FlH6vMJdnVs0qPNLd/receipts failed: Missing or insufficient permissions.
Here is the rule for firestore emulator
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
I don’t understand what I could be missing, it must be something wrong on the iOS side as on Android I’m able to listen to the query without any issue and retrieve the data.
Is there anything that I might have missed? Or a way to obtain more data on what’s exactly wrong?
2
Found the root cause, it looks like it comes from the kotlin gitlive-firebase library…. Remove this.
Firebase.firestore.settings = firestoreSettings {
}
Will probably ask in their github project why this happens.