我正在Swift中建立一个股票扩展名,该扩展名将文档保存到Firestore。到目前为止,我已经能够通过键链共享和应用程序组对正确的用户进行身份验证。我还可以从新文档参考获得DocumentID:
var ref = Firestore.firestore().collection("stuff").document()
print(ref.documentID) //prints the id
但是,当我尝试将某些东西保存到Firestore时,没有什么都在控制台中打印出来,这意味着我既没有从Firebase获得失败或成功回调(请参见下面的我批量更新(。这是我的sharecontroller.swift文件:
class ShareViewController: SLComposeServiceViewController {
var sharedIdentifier = "asdf"
override func viewDidLoad() {
FirebaseApp.configure()
setupKeychainSharing()
}
func setupKeychainSharing() {
do {
try Auth.auth().useUserAccessGroup(sharedIdentifier)
} catch let error as NSError {
}
}
override func isContentValid() -> Bool {
return true
}
override func didSelectPost() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
if let contents = content.attachments {
for attachment in contents {
if attachment.hasItemConformingToTypeIdentifier(m4aType) {
attachment.loadItem(forTypeIdentifier: m4aType, options: nil, completionHandler: { (results, error) in
if error == nil {
if let url = results as? URL {
if let audioData = NSData(contentsOf: url) {
let fileName = url.lastPathComponent
if Auth.auth().currentUser != nil {
guard let myId = Auth.auth().currentUser?.uid else { return }
let batch = Firestore.firestore().batch()
let ref = Firestore.firestore().collection("projects").document()
let project: [String: Any] = [
"ownerId": myId,
"type" : "audio",
"extensionUrl" : audioUrl.absoluteString
]
batch.updateData(project, forDocument: ref)
let privateRef = Firestore.firestore().collection("user-private").document(myId)
let privateUpdate: [String: Any] = [
"projects" : FieldValue.arrayUnion([ref.documentID])
]
batch.updateData(privateUpdate, forDocument: privateRef)
batch.commit(completion: { (error) in
if let error = error {
print("error updating database: (error.localizedDescription)")
} else {
print("Database updated successfully!!!!!")
self.extensionContext!.completeRequest( returningItems: [], completionHandler: nil)
}
})
}
}
}
}
})
}
}
}
}
}
}
看来您正在尝试在项目节点中创建其他文档并更新用户私人节点。如果是这样,问题中的代码将不起作用。
更新:更新文档中文档中的字段。如果 文档不存在,写入批次将失败。
这是一个工作批处理写入功能,它将文档添加到带有firestore生成的文档ID和子字段的项目节点,用于扩展,所有者和类型,以及带有ID_0 document ID的文档的User_private Collection。
func batchWrite() {
let batch = self.db.batch()
let ref = self.db.collection("projects").document()
let project: [String: Any] = [
"ownerId": "id_0",
"type" : "audio",
"extensionUrl" : "some url"
]
batch.setData(project, forDocument: ref)
let privateRef = self.db.collection("user-private").document("id_0")
let privateUpdate: [String: Any] = [
"projects" : "some projects"
]
batch.setData(privateUpdate, forDocument: privateRef)
batch.commit(completion: { (error) in
if let error = error {
print("error updating database: (error.localizedDescription)")
} else {
print("Database updated successfully!!!!!")
}
})
}
请注意,self.db是对我的firestore的类VAR引用。这使得您不必继续输入Firestore.firestore()
并改用self.db
。
还要注意,在这种情况下,可能不需要批处理,因为似乎没有大量的写入。
如果您不使用批处理,则.adddocument将在集合中添加文档。
以下是将任务写入任务集合并自动生成DocumentIDID
的函数func writeTask() {
let collectionRef = self.db.collection("tasks")
let data = [
"task": "some task"]
var ref: DocumentReference? = nil
ref = collectionRef.addDocument(data: data, completion: { err in
if let error = err {
print(error.localizedDescription)
}
print(ref?.documentID)
})
}
可能没有为您的共享扩展名配置firebase。打印语句不适用于共享扩展。相反,您应该使用NSLOG语句,即NSLog("refDocId:(ref.DocumentId)")
。然后在您的Xcode中,导航到Window>设备和模拟器>打开控制台。要在您的共享扩展程序中配置Firebase,请使用FirebaseApp.configure()