Skip to content

Completion handlers defaulting to main thread #9276

@thecoolwinter

Description

@thecoolwinter

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 13.2 (13C90)
  • Firebase SDK version: 8.12.0
  • Installation method: Swift Package Manager (select one)
  • Firebase Component: Firestore
  • Target platform(s): All

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

Full disclosure beforehand, this may not be a bug, this may be intentional but I can't find anything saying so.

The main methods in Firestore automatically reenter the main thread when completed. So any work that may be intended to be on a background thread (eg, the getDocuments method was called on the background thread) will be put on the main thread instead.

This can cause confusion when a method like Firestore.firestore().getDocuments was called on a background thread already, and wastes time reentering the main thread. This also produces a small amount of spaghetti code to bring the work back onto the original thread the method was called from.

Intended Functionality

When a method is called, it reenters the thread from before when completing the work Firestore had to do. Example in the Relevant Code section

Relevant Code:

The current method to make this happen as intended.

// Enter the background thread
DispatchQueue.global(qos: .background).async { [weak self] in
  // Make a query (in this example querying some Employees documents)
  Firestore.firestore().collection("Employees").getDocuments { [weak self] (snapshot, error) in
    // Ugly extra DispatchQueue call just to reenter the thread we were already on
    DispatchQueue.global(qos: .background).async { [weak self] in
      // do the work now on the background thread 
    }
  }
}

The intended method:

DispatchQueue.global(qos: .background).async { [weak self] in
  Firestore.firestore().collection("Employees").getDocuments { [weak self] (snapshot, error) in
    // Do the work on the background thread it was called from
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions