Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirebaseDataEncoder that encode Data when calling to encode, returns Any instead of Data #14045

Open
jesus-mg-ios opened this issue Nov 7, 2024 · 6 comments
Assignees

Comments

@jesus-mg-ios
Copy link

Description

Seems like there's a bug in the sign of FirebaseDataEncoder encoder method
open func encode<T : Encodable>(_ value: T) throws -> Any {
that should be
open func encode<T : Encodable>(_ value: T) throws -> Data {

because it encodes data.

Any cannot be sendable whereas Data it is.

Thanks for your time.

Reproducing the issue

No response

Firebase SDK Version

11.3

Xcode Version

16.1

Installation Method

Swift Package Manager

Firebase Product(s)

Firestore, Functions

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

No response

If using CocoaPods, the project's Podfile.lock

No response

@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jesus-mg-ios
Copy link
Author

jesus-mg-ios commented Nov 7, 2024

I can see now why it returns Any. It doesn’t actually encode to Data; instead, it generates something like an NSDictionary, parses it as an AnyObject, and then returns it as Any, right?

If that's the case, it would be great to return a SendableType as a wrapper to manage it, and manage it internally when calling a function.

@mortenbekditlevsen
Copy link
Contributor

It does encode. It encodes to a format that can be consumed by the realtime database, functions and firestore. Encoding does not impose any restrictions on the type of the encoded value.

It could probably be restricted to some sendable type though, but one could imagine that would require quite a bit of work.

Where would it be usable for the encoded value to be sendable?

@jesus-mg-ios
Copy link
Author

jesus-mg-ios commented Nov 8, 2024

let data = try firebaseEncoder.encode(dummyEncodableObject)
Task { @Sendable [weak function] in
   _ = try await function(#Call to the function#).call(data) // Capture of 'data' with non-sendable type 'Any' in a `@Sendable` closure; this is an error in the Swift 6 language mode 
}

Thanks for your reply! What I meant is that just by reading the name, it seems like FirebaseDataEncoder encodes to a Data type. For example, Foundation.JSONEncoder clearly encodes something to JSON data, being the representation Data type. So, the name FirebaseDataEncoder could be ambiguous, as it suggests it encodes something into "Firebase Data," with "data" potentially being interpreted as the Data type. A clearer, less ambiguous name could be FirebaseEncoder, which would suggest that the encoder prepares data for Firebase without implying its encoding to a specific Data type.

@milaGGL milaGGL self-assigned this Nov 8, 2024
@milaGGL
Copy link
Contributor

milaGGL commented Nov 8, 2024

@jesus-mg-ios , I wonder what is the error in Swift 6 language mode. Feels like this is similar to the issue:
#13962

@jesus-mg-ios
Copy link
Author

@milaGGL

 // Capture of 'data' with non-sendable type 'Any' in a `@Sendable` closure; this is an error in the Swift 6 language mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants