firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

[Firestore] Add async counterpart for the `addDocument` method

Open MojtabaHs opened this issue 1 year ago • 8 comments

The async version of the addDocument function exists only for Dictionary input, so I have added a codable version as well using the original existing methods.

  • The function signature is from the original; I just converted the completion parameter to async.
  • The documentation is from the original; I only added the Throws line from another existing function.
  • The implementation is from another existing function, but it uses the original addDocument which takes an encodable.

MojtabaHs avatar Jul 26 '24 11:07 MojtabaHs

Thanks @MojtabaHs! I'd rather see this added to Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift since that file is already Swift and it would be closer to the non-async implementation.

@ncooke3 @wu-hui Do you know if it was an oversight that this is missing or some other reason?

paulb777 avatar Jul 26 '24 22:07 paulb777

@MojtabaHs Thanks for the PR. Given the open questions we're seeing, we want to a deeper review of the Firestore APIs and make sure we're being consistent with how we handle async and throwing. It may take us a while longer to move forward here.

paulb777 avatar Jul 31 '24 00:07 paulb777

@paulb777 Let me know if there is anything I can do to help this progress.

MojtabaHs avatar Jul 31 '24 10:07 MojtabaHs

Any updates on this? There are other opportunities to introduce async counterparts to modernize the API, but I’m concerned that this decision could affect those efforts as well. I believe it’s important to address this first.

MojtabaHs avatar Aug 11 '24 07:08 MojtabaHs

Hi @MojtabaHs, no updates as of yet. This is something actively being looked into. I'll update when the direction is more clear.

ncooke3 avatar Aug 12 '24 13:08 ncooke3

It's been a while, how can I assist you in moving forward with this progress?

MojtabaHs avatar Sep 17 '24 05:09 MojtabaHs

Rebased on the current main branch.

Any updates? It’s been a while, and I’m concerned about losing context.

MojtabaHs avatar Nov 06 '24 10:11 MojtabaHs

Sorry for the delay here. We're focusing on Swift Concurrency improvements in other Firebase products before getting back to Firestore. It may be a few more months.

paulb777 avatar Nov 13 '24 23:11 paulb777

Rebased on the current main branch.

MojtabaHs avatar Mar 12 '25 10:03 MojtabaHs

As the poster of #9157 , I strongly discourage adding this API (for the same reason as doing it in RTDB).

Having an async function that never returns (when you're offline) could be highly unexpected. How a never returning async call would propagate to also let all callers never return would seem like a really hard issue to debug.

Using a callback closure suggests that this is perhaps a power user API that should be considered carefully...

@MojtabaHs , you mention the possibility of deprecating the existing async API that takes a Dictionary of data. I think that this is the better approach.

I assume that the existence of the async API is due to how Objective-C automatically imports to Swift, and it could be that this API has just not been vetted for adding attributes to explicitly not add an async version of the API.

I'd much rather see an entirely new API in the shape:

func addDocument(..) async throws -> SyncResult {}

enum SyncResult {
  case enqueued
  case synchronized
}
  • or something to that effect - although I don't know if the underlying technology would support this.

mortenbekditlevsen avatar Mar 12 '25 12:03 mortenbekditlevsen

@mortenbekditlevsen I read through the discussion on the issue you referenced... someone there suggested returning an AsyncStream, which could emit events with additional details (e.g. to inform the caller that the client is offline and this will be tried later). That sort of implementation seems like it would provide both swift concurrency support and address the issues you're calling attention to here.

Sam-Spencer avatar Mar 12 '25 16:03 Sam-Spencer

@Sam-Spencer It sounds like that could be a really great way to model that API.

Perhaps there could also be an event representing the situation where your write was made redundant by a later write or a remove - all while you were offline.

mortenbekditlevsen avatar Mar 13 '25 14:03 mortenbekditlevsen