Search code examples
swiftfirebasegoogle-cloud-firestore

Firebase Firestore @DocumentID not conforming to Sendable


I've enabled Xcode 'strict concurrency checking' and am now getting warnings on the sample data model of a Post.

struct Post: Identifiable, Codable {
    @DocumentID var id: String?
    var title: String
    var content: String
    var timestamp: Date
}

func firebaseQuery(queries: [String], queryField: String) async throws -> [Post] {
        
   var results = [Post]()
        
   try await withThrowingTaskGroup(of: [Post].self) { group in
       for query in queries {
           group.addTask { [self] in
               return try await getPostsFromFirebase(query: query, queryField: queryField)
           }
        }
            
        for try await result in group {
           results.append(contentsOf: result)
        }
   }
        
   return results
}

Warning "Type 'Post' does not conform to the 'Sendable' protocol"

Adding Sendable compliance to Post gives this warning:

Stored property '_id' of 'Sendable'-conforming struct 'PostSendable' has non-sendable type 'DocumentID'

How do I make Post comply to Sendable, or work with it not being Sendable?


Solution

  • For now, you can just mark the the import statement with preconcurrency:

    @preconcurrency import FirebaseFirestore
    

    When they get around updating the FirebaseFirestore module, you will either be able to remove the preconcurrency attribute if DocumentID is indeed sendable, or you will get the same warning again. See https://developer.apple.com/videos/play/wwdc2022/110351/ around 26:00 for details.