本文介绍了Swift loadItem关闭未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个共享扩展名,但是我捕获并保存共享附件的闭包没有运行.我如何找出原因?switch分支执行,附件在那里.没有错误消息,它永远不会运行.

I am writing a share extension, but my closure that would capture and save the shared attachment is not running. How can I find out why? The switch branch executes, the attachment is there. There is no error message, it just never runs.

 if let contents = content.attachments as? [NSItemProvider] {
   for attachment in contents {
     let fType = attachment.registeredTypeIdentifiers[0]
     if attachment.hasItemConformingToTypeIdentifier(fType) {
       switch fType {
         case kUTTypeImage as String as String: 
           do {
             attachment.loadItem(forTypeIdentifier: fType, options: nil, completionHandler: { data, error in
               print("AppImage")
               let url = data as! URL
               if let imageData = try? Data(contentsOf: url) {
                 self.appImage = UIImage(data: imageData)
                 self.saveImage(image: self.appImage!)
               }
             })
           } // public image case

推荐答案

一旦执行completeRequestReturningItems(CRRI),将不再调用您的loadItem的完成处理程序(此时它们将被有效地取消).因此,您必须同步异步任务,以确保在完成处理程序完成或不再关心之前不执行CRRI.从您的评论看来,您好像正在调用loadItem并立即继续调用CRRI.

Once completeRequestReturningItems (CRRI) is executed, your completion handlers for loadItem will no longer be called (they are effectively canceled at that point). Therefore you must synchronize your asynchronous tasks to ensure that you don't execute CRRI until your completion handlers have finished or until you no longer care. From your comments, it sounds like you are invoking loadItem and immediately proceeding to call CRRI.

请参阅以下相关问题的答案: iOS 8共享扩展名为loadItemForTypeIdentifier:options:completionHandler:完成关闭未执行

See answers to this related question: iOS 8 Share extension loadItemForTypeIdentifier:options:completionHandler: completion closure not executing

我更喜欢在那里使用调度组的答案.

I prefer the answer there that uses a dispatch group.

这篇关于Swift loadItem关闭未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:07