ios – Firebase: Not Writing to Firestore (Swift)

[ad_1]

I am working on developing an iOS app that connects to Firebase, and suddenly I am experiencing problems trying to write to Firestore. I am able to read data from Firestore, but whenever I attempt to write by calling db.collection("collectionName").document("documentId").setData(dataDict) {}, the following now happens:

  • A new document is NOT added to the collection and none of the document’s data (dataDict) is written. This was confirmed by checking the Firebase console.
  • .setData()‘s callback function is NEVER entered. This was confirmed by setting a breakpoint on the first line of code inside the callback function and noticing that the code never breaks here. This was also confirmed by adding multiple print statements inside the callback function and noticing that they are never executed.
  • My write count increments RAPIDLY until I run out of writes for the day. I am still on Firebase’s Spark plan because it is just me currently on the app as I develop it. Before running the app, my write count is at 0. Then, I will run the app once and click on the button once that calls .setData(). I then leave the app running in hopes of entering the breakpoint, but I eventually get an error message in my Xcode debug area saying: WriteStream Stream error: 'Resource exhausted: Quota exceeded.'. Even though I only made one call to .setData() that does not seem to work and does not seem to actually write anything.

On button press, here is one example — BoardService.createBoard() — of a function that is called:

import Foundation
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseFirestore

class BoardService {
    static var db = Firestore.firestore()
    static var boardsCollection = db.collection("boards")
    
    // creates a board document in the boards collection
    static func createBoard(onSuccess: @escaping (_ board: Board) -> Void) {

        print("inside createBoard")

        // get the current user's Id
        guard let userId = Auth.auth().currentUser?.uid else { return }
        print(userId)
        
        // create a board Id
        let boardId = boardsCollection.document().documentID
        print(boardId)
        
        // set the board's information
        let board = Board.init(
            ownerId: userId,
            boardId: boardId,
            content: []
        )
        guard let boardDict = try? board.asDictionary() else { return }
        print(boardDict)
        
        // set the board document
        boardsCollection.document(boardId).setData(boardDict) { (error) in

            print("inside setData callback")

            if let error = error {
                print("board error")
                print(error.localizedDescription)
                return
            } else {
                print("board success")
                onSuccess(avatar)
            }
        }
    }
}

All of the print statements are printed as expected, expect those inside of .setData()‘s callback function.

It might also be relevant to add that this was previously working with no issues. The same code that is now giving me problems was working as expected last week. So I am unsure what suddenly changed.

Please advise on what could be happening / going wrong here.

[ad_2]

Source link

Leave a Reply

Your email address will not be published.