gpt4 book ai didi

ios - 我怎么知道何时可以安全地重用来自另一个进程的后台 NSURLSessionConfiguration id?

转载 作者:行者123 更新时间:2023-12-01 20:08:42 26 4
gpt4 key购买 nike

我正在构建一个应用程序,用户可以在其中从应用程序和共享扩展启动后台文件上传。用户应该能够监控从主应用程序上传的任何进度。

在从扩展程序启动上传的情况下,我需要创建一个后台 session 配置,其 id 与扩展程序中使用的相同,以获取委托(delegate)调用以监控应用程序中的进度等。

在应用程序中,直到扩展程序退出,我才能执行此操作。
苹果文档说 https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html

You must create exactly one session per identifier (specified when you create the configuration object). The behavior of multiple sessions sharing the same identifier is undefined.



我已经验证了这一点。如果我不关闭共享扩展,我可以在主应用程序中创建 session 而不会出现任何错误,但我不会收到任何委托(delegate)调用。
当我在切换回主应用程序之前关闭扩展程序时,我可以附加到同一个后台 session 并收到委托(delegate)调用。都好。

当我使用 completeRequestReturningItems:completionHandler: 关闭共享扩展时在 NSExtensionContext , 进程什么时候退出?

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSExtensionContext_Class/#//apple_ref/occ/instm/NSExtensionContext/completeRequestReturningItems:completionHandler :

提到

Calling this method eventually dismisses the app extension’s view controller.



“最终”不是很具体。我如何从主应用程序中确定扩展程序没有运行?

我想出的唯一解决方法是定期在共享容器中写入一个文件,并在超过该时间段的超时后让主upp首先拿起后台 session 。但这是一个丑陋的黑客。

如果上传很小,是否有可能在扩展进程运行时上传完成,以便我必须在扩展而不是主应用程序中处理上传的完成?

总结一下:您如何安全地将上传从应用程序扩展传输到应用程序?

最佳答案

奎因“爱斯基摩人”对这类问题进行了很好的讨论。 on the old Dev Forums here .

与您直接相关的部分是

In my tests I've noticed that some annoying behaviour falls out of this design: if you start a task from an extension, it's non-deterministic as to whether the app or extension gets the didCompleteWithError callback. If the task runs super quickly, the extension typically gets the callback. If the task takes longer, the system has time to terminate the extension and the app is resumed to handle it.

There's really no way around this. The workaround is to put the code that handles request completion in both your app and your extension (possibly reusing the code via a framework).

It would be nice if the extension could disconnect from the session immediately upon starting its request. Alas, that's not currently possible (rdar://problem/18748008). The only way to programmatically disconnect from the session is to invalidate it, and that either cancels all the running tasks (-invalidateAndCancel) or waits for them to complete (-finishTasksAndInvalidate), neither of which is appropriate.



太棒了,

is it possible that the upload finishes while the extension process is running so that I must handle the completion of the upload in the extension



是的。是的。你不喜欢“非确定性”的行为吗?

how do you safely transfer an upload from an app extension to an app?



当然听起来你不能。所以欺骗那里的雷达,然后耐心等待修复!

关于ios - 我怎么知道何时可以安全地重用来自另一个进程的后台 NSURLSessionConfiguration id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38117278/

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