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

FirestoreSwift Library: Setting a @DocumentId fails all future saves

Open mknippen opened this issue 3 years ago • 1 comments

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 13.2.1
  • Firebase SDK version: 8.12.1
  • Installation method: CocoaPods
  • Firebase Component: Firestore Swift
  • Target platform(s): iOS

[REQUIRED] Step 2: Describe the problem

Setting a @DocumentId fails all future saves. In the FirebaseFirestoreSwift library, there's a property wrapper for the document Id. It's a string, and appears settable. However, doing so causes all subsequent saves to fail with no error message or callback.

Use Case: I want a /users/ table, and I want the key to be their Firebase Auth Id. I create a new User object, set it, and set the Id. However, when I save, nothing happens.

Steps to reproduce:

See Sample code below. Create a user, set the id, and try to save.

Expected Results: It should appear in the database, or return an error message if it fails Actual Results: Nothing happens. No failure in a do/catch block either

Relevant Code:

import Firebase
import FirebaseFirestoreSwift

struct User: Codable {
    @DocumentID var id: String?
    var name: String
    var imageURL: URL?
    var location: String?
    @ServerTimestamp var createdAt: Date?
    @ServerTimestamp var updatedAt: Date?
}

let user = User(name: "Matthew")
user.id = "ANYTHING"
let ref = Firestore.firestore().collection("users").document(userId)
try! ref.setData(from: self)

mknippen avatar Feb 19 '22 17:02 mknippen

cc @peterfriese

ryanwilson avatar Feb 23 '22 15:02 ryanwilson

Hi @mknippen,

Thank you for the detailed report and sorry for the delay. Do you happen to happen to have a more concrete code sample? I haven't been able to reproduce the issue but I can't directly use your included code so there may be something relevant that I'm missing.

let user = User(name: "Matthew")
user.id = "ANYTHING"  // `user` is a constant, is it `var user` in the real code?
// I'm not sure where `userId` comes from but it will take precedence over `user.id` in the `user` object
let ref = Firestore.firestore().collection("users").document(userId)
try! ref.setData(from: self)  // I'm not sure the type of `self`, is this `user` in the real code?

I tried the following code and the user Foo was successfully written to users/ghi789:

struct User: Codable, Identifiable {
  @DocumentID var id: String?
  var name: String
  var imageURL: URL?
  var location: String?
  @ServerTimestamp var createdAt: Date?
  @ServerTimestamp var updatedAt: Date?
}

class AppDelegete: NSObject, UIApplicationDelegate {
  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication
                    .LaunchOptionsKey: Any]? = nil) -> Bool {
    FirebaseApp.configure()
    
    var user = User(name: "Foo")
    user.id = "def456"  // This line is ignored
    
    let ref = Firestore.firestore().collection("users").document("ghi789")
    try! ref.setData(from: user)
    // User is written to "users/ghi789"
    
    return true
  }
}

Unless there's an API I don't know about for inserting values into Firestore, we may want to consider making the @DocumentID setter private.

andrewheard avatar Sep 02 '22 23:09 andrewheard

Hey @mknippen. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Sep 09 '22 01:09 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@mknippen if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Sep 15 '22 01:09 google-oss-bot