1
votes

I'm working on swift programming but every time I run my app, it suddenly starts crashing. The error message I got was

Thread 1: Exception: "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1"

I searched the error and people are saying if I have several collections and documents in db, it tends to happen. But my db is really simple. only two collections and inside of them I have documents. Like this image. db image

When I tap a bar button, the app crashes, which is this lines of code.

@IBAction func addPressed(_ sender: UIBarButtonItem) {
    
    performSegue(withIdentifier: K.homeToGameScreen, sender: self)
    
    // create new game array in db & player's ready status -> true
    
    db.collection(K.FStore.newGameCollection)
        .addDocument(data: [K.FStore.gameBoardField: GameBoard.gameBoard, K.FStore.player1Field: playerInfo[K.FStore.nameField]!, K.FStore.player2Field: K.FStore.player2Field, K.FStore.uID: playerInfo[K.FStore.uID]!]) { (err) in
            
            if let err = err {
                print("Error getting documents3: \(err)")
            } else {
                
                self.db.collection(K.FStore.playersCollection).document(self.docId).updateData([K.FStore.isReadyField: true]){ err in
                    if let err = err {
                        print("Error updating player's isReady status: \(err)")
                    } else {
                        print("Document successfully updated")
                    }
                }
            }
    }
}

the code above are in something called HomeViewController, but I always get error in AppDelegate.swift file.

Could anyone please tell me what is going on here and how to fix this please?

1
Did you find the way out evetually?Memphis Meng

1 Answers

3
votes

The problem is almost certainly that self.docId is nil or empty string at the time it's being used, which prevents it from creating a path segment for the code that's building the DocumentReference. We can't see how you assigned it or even where it's defined, so you'll have to debug this and figure out why it doesn't have the value you expect.