Search code examples
firebaseauthenticationgoogle-cloud-firestorelogoutcompletion

Firestore call setData will not enter completion block after signOut and signIn firebase auth on iOS


In iOS (swift) with Firebase SDK

Using Firebase (4.10.0)

Using FirebaseAuth (4.4.4)

Using FirebaseCore (4.0.16)

Using FirebaseFirestore (0.10.2)

Problem happen when

  1. signIn (anonymous or with credential)
  2. signOut
  3. signIn
  4. call FIRDocumentReference.setData then completion block of setData will never called

Problem will not happen if

  1. signOut
  2. terminate app
  3. signIn
  4. call FIRDocumentReference.setData everything work fine

import UIKit
import Firebase

class ViewController: UIViewController {

    let mEmail = "[email protected]"
    let mPassword = "xxxxx"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func clickSignIn(_ sender: Any) {
        print("clickSignIn")
        Auth.auth().signIn(withEmail: mEmail, password: mPassword) { (user, error) in
            print("user:", user ?? "no user")
            print("error:", error ?? "no error")
        }
    }

    @IBAction func clickSignOut(_ sender: Any) {
        print("clickSignOut")
        do {
            try Auth.auth().signOut()
        } catch {
            print("can't signOut")
        }
        print(Auth.auth().currentUser ?? "no user")
    }

    @IBAction func clickSetData(_ sender: Any) {
        print("clickSetData")
        let ref = Firestore.firestore().document("tests/test_1")
        let data = ["text": "xxxxx"]
        ref.setData(data, options: SetOptions.merge()) { (e) in
            if (e == nil) {
                print("setData complete")
            } else {
                print("setData error:", e ?? "no error")
            }
        }
    }

}

And sometime it cause this crash too

*** Assertion failure in -[FSTLevelDBMutationQueue removeMutationBatches:group:], third_party/firebase/ios/Source/Firestore/Source/Local/FSTLevelDBMutationQueue.mm:536
2018-03-08 03:56:22.364597+0700 setdata-bug[30957:9502011] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Mutation batch [mutation: userID=\^X8I\^G\^A batchID=5] not found; found [mutation: userID=P¸P››\^? batchID=0]'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010a05c12b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00000001096f0f41 objc_exception_throw + 48
	2   CoreFoundation                      0x000000010a0612f2 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x0000000109191d69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
	4   setdata-bug                         0x0000000107333229 -[FSTLevelDBMutationQueue removeMutationBatches:group:] + 1967
	5   setdata-bug                         0x000000010733fc8e -[FSTLocalStore removeMutationBatches:group:] + 885
	6   setdata-bug                         0x000000010733f753 -[FSTLocalStore releaseBatchResults:group:remoteDocuments:] + 537
	7   setdata-bug                         0x000000010733c4f7 -[FSTLocalStore acknowledgeBatchWithResult:] + 571
	8   setdata-bug                         0x0000000107321e36 -[FSTSyncEngine applySuccessfulWriteWithResult:] + 166
	9   setdata-bug                         0x0000000107363325 -[FSTRemoteStore writeStreamDidReceiveResponseWithVersion:mutationResults:] + 300
	10  setdata-bug                         0x0000000107373069 -[FSTWriteStream handleStreamMessage:] + 847
	11  setdata-bug                         0x00000001073712da -[FSTStream writeValue:] + 477
	12  setdata-bug                         0x000000010736ee73 -[FSTCallbackFilter writeValue:] + 84
	13  RxLibrary                           0x00000001086065d6 __57-[GRXConcurrentWriteable enqueueValue:completionHandler:]_block_invoke + 86
	14  libdispatch.dylib                   0x000000010b0cd2f7 _dispatch_call_block_and_release + 12
	15  libdispatch.dylib                   0x000000010b0ce33d _dispatch_client_callout + 8
	16  libdispatch.dylib                   0x000000010b0d6855 _dispatch_queue_serial_drain + 1162
	17  libdispatch.dylib                   0x000000010b0d71ea _dispatch_queue_invoke + 336
	18  libdispatch.dylib                   0x000000010b0d2f7c _dispatch_queue_override_invoke + 733
	19  libdispatch.dylib                   0x000000010b0da102 _dispatch_root_queue_drain + 772
	20  libdispatch.dylib                   0x000000010b0d9da0 _dispatch_worker_thread3 + 132
	21  libsystem_pthread.dylib             0x000000010d8051ca _pthread_wqthread + 1387
	22  libsystem_pthread.dylib             0x000000010d804c4d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException


Solution

  • It was confirmed by google engineers that the issue I've identified is a known bug on the latest iOS SDK version (4.10.0). The good news is that, it was already fixed in the following pull requests: 890, 893. Until the fixes has been publicly released, Google suggest us to temporarily downgrade to version 4.9.0 for now. Any specifics or timeline can't be shared at the moment so please keep an eye out on our release notes for any updates.