URLSession.dataTask(with:completionHandler :)是否总是只调用一次completeCompleteHandler?

时间:2019-02-11 02:07:47

标签: ios swift nsurlsession nsurlsessiondatatask

在我使用URLSession.dataTask(with:completionHandler:)创建新的会话数据任务并通过调用其<IfModule mod_ssl.c> <VirtualHost *:443> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName www.orderfetchers.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf Alias /static /home/abdul/Fetchors/static <Directory /home/abdul/Fetchors/static> Require all granted </Directory> Alias /media /home/abdul/Fetchors/media <Directory /home/abdul/Fetchors/media> Require all granted </Directory> <Directory /home/abdul/Fetchors/Fetchors> <Files wsgi.py> Require all granted </Files> </Directory> WSGIScriptAlias / /home/abdul/Fetchors/Fetchors/wsgi.py WSGIDaemonProcess django_app python-path=/home/abdul/Fetchors python-home=/home/abdul/Fetchors/venv WSGIProcessGroup django_app Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/www.orderfetchers.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.orderfetchers.com/privkey.pem </VirtualHost> </IfModule> 方法来启动任务之后,鉴于该应用程序在请求运行时不会崩溃,对我来说是否安全假设resume()(传递给上面的completionHandler)最终总是只会被调用一次,即使网络请求(例如连接断开)或应用程序(例如,进入后台)?

注意:我没有在任务上显式调用URLSession.dataTask(with:completionHandler:)cancel()。只是suspend()

我想知道这个问题的答案,因为(正在我的应用程序的主线程中)我正在创建并启动(一个接一个)多个异步网络请求,并且想知道最后一个请求何时完成。

具体地说,我正在开发一个具有名为resume()的自定义类的应用程序。在启动时,该应用程序(假设它找到了存储在Account中的帐户访问令牌)仅创建该类的一个实例,并将其存储到一个名为UserDefaults的全局变量(跨整个应用程序),该变量代表该应用程序的当前登录帐户。

我已将名为account(例如)的var(实例)存储属性添加到Account,并默认将其设置为pendingGetFooRequestCount。每次调用0(一种实例方法)时,我都会在Account.getFoo()上添加1(在调用pendingGetFooRequestCount之前)。在resume()内部(传递给completionHandler和(为了安全起见)在传递给URLSession.dataTask(with:completionHandler:)的闭包内部,我首先从DispatchQueue.main.async()中减去1,然后检查是否{ {1}}等于pendingGetFooRequestCount。如果是这样,我知道上一个get-foo请求已完成,因此我可以调用另一种方法来继续流程。

我的逻辑如何?会按预期工作吗?我应该以其他方式这样做吗?另外,我是否甚至需要在主线程上减少pendingGetFooRequestCount

1 个答案:

答案 0 :(得分:1)

URLRequest具有timeoutInterval属性,其默认值为60秒。如果此时没有响应,则以非nil错误调用完成。