gpt4 book ai didi

ios - URLSession 对象是资源密集型的吗?

转载 作者:行者123 更新时间:2023-11-28 07:22:43 27 4
gpt4 key购买 nike

为每个网络请求创建一个新的 URLSession 是否会占用大量资源?

一些背景:

我正在开发一个用于发出网络请求的库。我正在尝试添加一项功能,允许将结果下载到一个文件中,该文件也会报告其进度。为此,我将不得不成为 session 的代表。

这没什么大不了的,除非公共(public)接口(interface)允许自定义用于请求的 URLSession。我不想覆盖开发人员想要用自己的委托(delegate)进行的任何自定义。

现在,我在想这样做的方法是 secret 地复制他们认为正在使用的 session (是的,我要做的不仅仅是复制对象本身)然后我的内部委托(delegate)将调出原始公共(public) session 的方法。如果他们在请求期间尝试操纵 session ,仍然可能会出现困惑/问题,但这似乎是一个小得多的边缘案例。

我现在唯一担心的是,如果发出很多请求,这可能会占用大量资源。有人对此有感觉吗?

最佳答案

是的,它们很密集。这是来自 Apple Staff on the developer forums 的引述.

This is a common anti-pattern, one that we specifically warned against at at WWDC this year. Creating a session per request is inefficient both on the CPU and, more importantly, on the network. Specifically, it prevents connection reuse, which can radically slow down back-to-back requests. This is especially bad for HTTP/2. We encourage folks to group all similar tasks in a single session, using multiple sessions only if you have different sets of tasks with different requirements (like interactive tasks versus background download tasks). That means that many simple apps can get away with using a single statically-allocated session.

关于ios - URLSession 对象是资源密集型的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634727/

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