Action Extension for Safari: Why reacts the done button with a delay

In my content blocker app I added an action extension for Safari. When I tap on the close button of my action extensions view controller the view disappears but with a big delay. Sometimes the view starts the closing animation after 2-3 seconds and sometimes directly. I removed all of the code inside the buttons delegate method so when the user taps the button only self.extensionContext!.completeRequestReturningItems will be called where the completion block is empty. But this has the same effect.


The strange thing is that I dont see this delay in other Safari extensions. Their views vanish directly when you press the close button.


Has someone of you an idea what the reason is for this delay? This app and the action extension I wrote completely in Swift.

Hey, I have the same problem. Have you found a solution maybe?

One cause could be that you are passing self.extensionContext.inputItems to the items argument of -completeRequestReturningItems:completionHandler: (the code in the Action Extension template does this). Or more generally, you are accessing self.extensionContext.inputItems somewhere along the dismissal logic in your extension.


Accessing self.extensionContext.inputItems in an action extension presented by Safari is expensive because it involves executing the Javascript in your extension's NSExtensionJavaScriptPreprocessingFile (for each access). You should limit reads from self.extensionContext.inputItems to once in -beginRequestWithExtensionContext:, or otherwise cache the result.


So what should you pass for the items argument of -completeRequestReturningItems:completionHandler: then? If the code in your NSExtensionJavaScriptPreprocessingFile does not have a finalize()function, then an emptry array will suffice. Otherwise you should pass whatever data is needed by your finalize() function.

Action Extension for Safari: Why reacts the done button with a delay
 
 
Q