将文档的文档 ID 添加到其自己的 Firestore 文档 - Swift 4

yam*_*mbo 9 ios firebase swift google-cloud-firestore

如何将刚刚添加到我的 firestore 数据库中的文档的文档 ID 添加到所述文档?

我想这样做,以便当用户检索“乘车”对象并选择预订它时,我可以知道他们预订了哪个特定的乘车。

我面临的问题是在创建文档之前您无法获取文档 ID,因此将其添加到所述文档的唯一方法是创建文档,读取其 ID,然后编辑文档以添加身份证。在规模上,这将根据需要创建两倍的服务器调用。

有没有标准的方法来做到这一点?或者一个简单的解决方案来知道用户预订了哪个“乘车”并在数据库中相应地对其进行编辑?

struct Ride {
    var availableSeats: Int
    var carType: String
    var dateCreated: Timestamp
    var ID: String // How do I implement this?
}


func createRide(ride: Ride, completion: @escaping(_ rideID: String?, _ error: Error?) -> Void) {
    // Firebase setup
    settings.areTimestampsInSnapshotsEnabled = true
    db.settings = settings

    // Add a new document with a generated ID
    var ref: DocumentReference? = nil
    ref = db.collection("rides").addDocument(data: [
        "availableSeats": ride.availableSeats,
        "carType": ride.carType,
        "dateCreated": ride.dateCreated,
        "ID": ride.ID,
    ]) { err in
        if let err = err {
            print("Error adding ride: \(err)")
            completion(nil, err)
        } else {
            print("Ride added with ID: \(ref!.documentID)")
            completion(ref?.documentID, nil)
            // I'd currently have to use this `ref?.documentID` and edit this document immediately after creating. 2 calls to the database.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jay*_*Jay 13

虽然有一个完美的答案,但 FireStore 具有您需要的内置功能,并且不需要两次调用数据库。事实上,它不需要对数据库进行任何调用。

这是一个例子

    let testRef = self.db.collection("test_node")
    let someData = [
        "child_key": "child_value"
    ]

    let aDoc = testRef.document() //this creates a document with a documentID
    print(aDoc.documentID) //prints the documentID, no database interaction
    //you could add the documentID to an object etc at this point
    aDoc.setData(someData) //stores the data at that documentID
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅文档添加文档

在某些情况下,使用自动生成的 ID 创建文档引用,然后稍后使用该引用会很有用。对于此用例,您可以调用 doc():

您可能需要考虑一种稍微不同的方法。您也可以在写入后的闭包中获取文档 ID。所以让我们给你一个酷骑(类)

class RideClass {
    var availableSeats: Int
    var carType: String
    var dateCreated: String
    var ID: String

    init(seats: Int, car: String, createdDate: String) {
        self.availableSeats = seats
        self.carType = car
        self.dateCreated = createdDate
        self.ID = ""
    }

    func getRideDict() -> [String: Any] {
        let dict:[String: Any] = [
            "availableSeats": self.availableSeats,
            "carType": self.carType,
            "dateCreated": self.dateCreated
        ]
        return dict
    }
}
Run Code Online (Sandbox Code Playgroud)

然后一些代码来创建骑行,写出来并利用它的自动创建的文档ID

    var aRide = RideClass(seats: 3, car: "Lincoln", createdDate: "20190122")

    var ref: DocumentReference? = nil
    ref = db.collection("rides").addDocument(data: aRide.getRideDict() ) { err in
        if let err = err {
            print("Error adding document: \(err)")
        } else {
            aRide.ID = ref!.documentID
            print(aRide.ID) //now you can work with the ride and know it's ID
        }
    }
Run Code Online (Sandbox Code Playgroud)