简体   繁体   English

网络丢失,屏幕锁定解锁

[英]Network lost on screen lock unlock

Encountered a very weird problem, using simple AFNetworking downloading operation, even tried with simple NSURLConnection operation, connection fails if you keep your app running, and lock screen and then unlock. 使用简单的AFNetworking下载操作遇到了一个非常奇怪的问题,甚至尝试了简单的NSURLConnection操作,如果保持应用程序运行,则连接失败,然后锁定屏幕然后再解锁。 Works absolutely fine in background though. 虽然在background工作正常。

Any one encountered similar problem with NSURLConnection want to share some solution? 任何人遇到NSURLConnection类似问题都想分享一些解决方案吗?

Thanks. 谢谢。

It looks like an iOS bug. 它看起来像一个iOS错误。 Weird, but lock screen action affects NSURLSession somehow, so that it stops working and returns NSURLErrorNetworkConnectionLost . 很奇怪,但是锁屏操作会以某种方式影响NSURLSession ,从而使其停止工作并返回NSURLErrorNetworkConnectionLost So in my app I gave up using shared session. 因此,在我的应用程序中,我放弃了共享会话。 I either use a new session object for every request or (if I need to maintain one session constantly) recreate it every time the screen gets unlocked. 我要么为每个请求使用一个新的会话对象,要么(每次需要保持一个会话时)每次解锁屏幕时都重新创建它。 And it works. 而且有效。 For users of AFNetworking or any other third party library working on top of NSURLSession the situation is harder, of course. 对于AFNetworking或在NSURLSession之上工作的任何其他第三方库的用户NSURLSession ,情况当然更难。 You'll need to correct the code of the library, which is definitely not a good thing, but I think there's no other choice 您需要更正库的代码,这绝对不是一件好事,但我认为别无选择

Very helpful Andrey Chernukha , In my case, figured out that you don't necessary need to recreate new session every time. 非常有用Andrey Chernukha在我的案例中,发现您不必每次都重新创建新会话。 I ended up using array to save running NSURLSessionDataTasks and after phone is unlocked resume them. 我最终使用数组保存了正在运行的NSURLSessionDataTasks,并在手机解锁后恢复了它们。

Steps: 脚步:

  1. I created array NSMutableArray *dataTasksToResume 我创建了数组NSMutableArray * dataTasksToResume

  2. In - (void)applicationWillResignActive:(UIApplication *)application I saved all tasks to dataTasksToResume array -(void)applicationWillResignActive:(UIApplication *)application我将所有任务保存到dataTasksToResume数组

  3. Cancel all running NSURLSessionDataTasks 取消所有正在运行的NSURLSessionDataTasks
  4. In - (void)applicationDidBecomeActive:(UIApplication *)application get all tasks from array and resuming them (re-creating them) -(void)applicationDidBecomeActive:(UIApplication *)应用程序中,从数组中获取所有任务并恢复它们(重新创建它们)
  5. Enjoy! 请享用!

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM