gpt4 book ai didi

ios - iOS 共享扩展中的 Firebase runTransactionBlock()

转载 作者:行者123 更新时间:2023-11-29 12:09:23 25 4
gpt4 key购买 nike

我的共享扩展包含以下代码作为 didSelectPost() 段的一部分:

override func didSelectPost() {
if self.sharedURL != nil {
// Send data to Firebase
self.myRootRef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? String
// Getting the current value
// and checking whether it's null
if value == nil {
value = ""
}
// Setting the new value to the clipboard
// content
currentData.value = self.sharedURL?.absoluteString

// Finalizing the transaction
return FTransactionResult.successWithValue(currentData)
}, andCompletionBlock: {
// Completion Check
(error:NSError!, success:Bool, data:FDataSnapshot!) in
print("DEBUG- We're done:\(success) and \(error)")
}
)
}

// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}

我在运行时遇到以下错误:

host connection <NSXPCConnection: 0x7fb84af2e8c0> connection from pid 16743 invalidated

我认为此错误是由于 andCompletionBlock 引起的,并且与以下问题有关:Debug info when run today extension

如何干净利落地处理上述交易的完成状态?

最佳答案

就像您链接到的答案一样,NSXPCConnection 错误在这里无关紧要。

问题是 .runTransactionBlock() 是异步的,并且 .completeRequestReturningItems() 将在您从 Firebase 数据库中获取值之前被调用并退出扩展。

尝试在 andCompletionBlock 中运行 .completeRequestReturningItems()

override func didSelectPost() {
if self.sharedURL != nil {
// Send data to Firebase
self.myRootRef.runTransactionBlock({
(currentData:FMutableData!) in
var value = currentData.value as? String
// Getting the current value
// and checking whether it's null
if value == nil {
value = ""
}
// Setting the new value to the clipboard
// content
currentData.value = self.sharedURL?.absoluteString

// Finalizing the transaction
return FTransactionResult.successWithValue(currentData)
}, andCompletionBlock: {
// Completion Check
(error:NSError!, success:Bool, data:FDataSnapshot!) in
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}
)
}

}

关于ios - iOS 共享扩展中的 Firebase runTransactionBlock(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33950411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com