首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Firebase runTransactionBlock()在iOS共享扩展中的应用

Firebase runTransactionBlock()在iOS共享扩展中的应用
EN

Stack Overflow用户
提问于 2015-11-27 04:45:56
回答 1查看 476关注 0票数 1

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

代码语言:javascript
运行
复制
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)
}

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

代码语言:javascript
运行
复制
host connection <NSXPCConnection: 0x7fb84af2e8c0> connection from pid 16743 invalidated

我相信这个错误是由于andCompletionBlock引起的,并且与以下问题有关:Debug info when run today extension

如何干净而成功地处理上述事务的完成状态?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-28 15:32:53

就像您链接到的答案一样,NSXPCConnection错误在这里并不重要。

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

尝试在.completeRequestReturningItems()中运行andCompletionBlock

代码语言:javascript
运行
复制
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)
                }
            )
        }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33950411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档