Learn Courses My Dashboard

Cannot write in Firebase db

hello Guys

I was working in lesson 5 firebase course and I cannot write into the db

The bundle matches


The googleservice-info.plist is in the workspace
image

the pod looks good

Pods for firebasedemo2

pod 'Firebase/Analytics'
pod 'Firebase/Auth'
pod 'Firebase/Core'
pod 'Firebase/Firestore'
pod 'FirebaseFirestoreSwift'

the red/write rule is enabled in firebase

This is the code:

import SwiftUI
import Firebase

@main
struct firebasedemo2App: App {
    
    
    init() {
        FirebaseApp.configure()
        //to initialize firebase
        makeReservation()
    }
    
    func makeReservation(){
        //access firebase db
        let db = Firestore.firestore()
        
        //reference to reservations collection
        let reservations = db.collection("reservations")
        
        //create a doc with given identitifer
        reservations.document("Test123").setData(
            ["name" : "Carol", "people" : 22])
        
        //create a doc with unique identifier
        reservations.document("").setData(["name" : "donald"])
        
        //create doc with a given data
        let document = reservations.addDocument(data: ["name" : "Perrito", "people" : 10])
    }

this is an “error” I get when compiling

can somebody help? I am sure it’s something simple…

Where is that error occurring? That code looks a bit like it is from within the Firebase Pod frameworks.

not sure, the build is succesful though

I deleted the project, created two more iOS projects to make sure I didn’t;t make a mistake and I get the same error and doesn’t write to the db

it shows NSException…

@Naticio

I’m guessing that you are doing the course named “iOS Databases (SwiftUI)” and you are stepping through Module 1. Is that correct?

If that is the case then the only pod you require in your Podfile definition for this demonstration project is:

pod 'Firebase/Firestore'

Correct, I’m doing lesson 5 of the firebase module

I modified the pod file including only pod ‘Firebase/Firestore’, did a pod update and still I cannot write into the db

I got a different exception though, not sure if it means anything

Can you manually create a document via your Firestore console?

Oh wait, did you say you deleted then created another project? Did you use the same project name or a different one? If so then your GoogleServices-Info.plist file will only match the original Bundle_ID.

yes, I can manually create docs in the Firebase console.

I created a 2nd demo project
image

then inside firebasedemo2 I deleted the iOS app because it was not working and created a new app and used that plist file

so the plist file only matches 1 bundle id?

so in that case, should I create a new project to get a brand new plist file?

thanks

@Naticio

If you look inside your GoogleServices-Info.plist file you will see a Key:value pair entry named BUNDLE_ID with something like com.Name.FirebaseDemo. I’m assuming that yours would be something like serrano.firebasedemo

All the rest of the Key:Value pairs in that file are related to the Google Firebase side of your project.

Make a note of that and create a new project with the original Project Name that you had.

You could create a brand new Xcode project and then create a New Firebase project with that BundleId and then download a new GoogleService-Info.plist file if you wanted to. I suppose it’s good practise to do that. If you do choose to create a new Firebase Project then delete the old one so that it does not count towards your maximum number of Firebase projects for free.

1 Like

thanks, I recreated the demo + firebase from scratch, I got the same error. I realized it was because I had an empty string in the doc value

incorrect:
reservations.document("").setData([“name” : “donald”])

correct:
reservations.document().setData([“name” : “donald”])

1 Like