问题描述
我已经创建了这样的结构:
struct TestObject: Codable {
public enum CodingKeys: String,CodingKey {
case ref = "r"
case string = "s"
}
@DocumentID
public var ref: DocumentReference?
public var string: String
public init(string: String) {
ref = nil
self.string = string
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
ref = try container.decodeIfPresent(DocumentReference.self,forKey: .ref)
string = try container.decode(String.self,forKey: .string)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(ref,forKey: .ref)
try container.encode(string,forKey: .string)
}
}
如果我随后将对象保存到 Firestore 并在快照侦听器中接收它,则 .ref
为 nil
:
self.firestore.collection("test").addSnapshotListener { (snapshot,error) in
if let snapshot = snapshot,let document = snapshot.documents.first
{
let testObject = try! document.data(as: TestObject.self)!
XCTAssertNotNil(testObject.ref) // <-- this is `nil`,but `document.documentID` is populated
}
}
是的 - 我已经验证 document.documentID
实际上已设置为快照。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)