URLSession最佳做法

编程入门 行业动态 更新时间:2024-10-28 21:15:05
本文介绍了URLSession最佳做法-多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一些有关URLSession/NSURLSession最佳实践的指导.

我的要求规定我必须执行临时/定期HTTP GET请求.即每隔几分钟,然后可能每30秒更改一次.

无论如何,我已经实现了以下目标,我有一个包含如下代码的方法:

let urlRequest = URLRequest(url: url) // set up the session let config = URLSessionConfiguration.default let session = URLSession(configuration: config) // make the request let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in ...

这会将URLSession保留在此方法的范围之内.

一些问题:

1)是实现我正在寻找的目标的正确方法吗?还是我应该在类级别的范围内拥有一个URLSession并创建多个任务"? -如果可以的话,我应该在每个任务上调用.resume吗?

2)如果这是URLSession的可接受用法范例,我是否在泄漏内存(对于ARC来说不这样)?我问,因为我注意到URLSession对象似乎不在每次调用/替换它的方法范围之内. (我使用对我定期检查的对象的弱引用来对此进行检查)...

3)有没有更整洁的方法来通过URLSession临时请求相同的URL?我觉得NSURLConnection处理得很好,但已弃用.

解决方案

1)是实现我正在寻找的东西的正确方法,还是我应该在类级别的范围内拥有一个URLSession并创建多个任务" '? -如果可以的话,我应该在每个任务上调用.resume吗?

answer: 尽管这很常见,但您所做的并非不正确. 最佳做法是使用URLSession的共享实例.为您提出的每个请求(通常每30秒)创建一个新的会话配置和URLSession对象,其性能要比使用单例的性能低.

2)如果这是URLSession的可接受用法范式,我是否在泄漏内存(ARC认为不对)?我问,因为我注意到URLSession对象似乎不在每次调用/替换它的方法范围之内. (我使用对我定期检查的对象的弱引用来对此进行检查)...

answer: 您可能会注意到URLSession对象超出了方法调用范围的原因是,您向其传递了一个转义的闭包,并且会话可能一直存在,直到调用完成处理程序为止.

3)是否有一种更整洁的方法来通过URLSession临时请求相同的URL?我觉得NSURLConnection处理得很好,但已弃用.

answer:有很多方法可以完成此任务.您可以尝试存储对URL或请求的引用,然后根据需要使用相同的请求调用方法.

I need a little guidance on best practice with URLSession/NSURLSession.

My requirement stipulates that I have to do adhoc/periodic HTTP GET requests. i.e. Every few mins, then maybe every 30 seconds, changing at will.

Anyway I've achieved this as follows, I have a method that contains code like this:

let urlRequest = URLRequest(url: url) // set up the session let config = URLSessionConfiguration.default let session = URLSession(configuration: config) // make the request let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in ...

That keeps the the URLSession within the scope of this method.

So some questions:

1) is this the right way to achieve what I'm looking for or should I have a URLSession at Class level scope and create multiple 'tasks'? - if so should I called .resume on each task?

2) if this is an acceptable usage paradigm for URLSession am I leaking memory (don't think so with ARC)? I ask as I've noticed that the URLSession object seems to stay around outside the scope of the method where I call it/replace it each time. (I check this with a WEAK reference to the object that I periodically inspect)...

3) is there a neater way to GET request the same URL on an adhoc basis with URLSession? I feel NSURLConnection handled this well but its deprecated.

解决方案

1) is this the right way to achieve what I'm looking for or should I have a URLSession at Class level scope and create multiple 'tasks'? - if so should I called .resume on each task?

answer: What you are doing is not incorrect, though it is uncommon. The best practice would be to use URLSession's shared instance. Creating a new session configuration and URLSession object for each request that you are making (as often as every 30 seconds), would be less performant than using the singleton.

2) if this is an acceptable usage paradigm for URLSession am I leaking memory (don't think so with ARC)? I ask as I've noticed that the URLSession object seems to stay around outside the scope of the method where I call it/replace it each time. (I check this with a WEAK reference to the object that I periodically inspect)...

answer: the reason that you may notice that your URLSession object is living beyond the scope of your method call is the you are passing it an escaping closure and the session will likely live until the completion handler is called.

3) is there a neater way to GET request the same URL on an adhoc basis with URLSession? I feel NSURLConnection handled this well but its deprecated.

answer: There are many ways to accomplish this. You might try storing a reference to either the URL or the request, and call your method with the same request as you wish.

更多推荐

URLSession最佳做法

本文发布于:2023-11-26 19:45:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:做法   URLSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!