为什么我的后台工作线程阻止UI线程?

时间:2021-04-04 20:57:35

I am working on an app, which uploads native contacts to server then get responses(JSON, a contact list that already installed the app). When native contacts are large enough, server response will be slow and unstable. And user cannot do other things. so I put network request into background thread. every time I will upload 100 contacts, do some tasks , then next 100 contacts until loop finish.

我正在开发一个应用程序,它将本机联系人上传到服务器然后获得响应(JSON,已经安装了应用程序的联系人列表)。当本机联系人足够大时,服务器响应将变得缓慢且不稳定。并且用户不能做其他事情。所以我把网络请求放到后台线程中。每次我上传100个联系人,做一些任务,然后接下来100个联系人,直到循环结束。

But in running, the result is not as expected. background thread is running, it keeps to request server. UI thread is blocked, I still cannot do anything.

但在跑步中,结果并不像预期的那样。后台线程正在运行,它继续请求服务器。 UI线程被阻止,我仍然无能为力。

is this cause a long loop in background thread? Although I have 2 thread, but they will compete CPU resources(test device is iPod, 1 core. And I think this may not related core numbers)?

这是导致后台线程中的长循环吗?虽然我有2个线程,但它们会竞争CPU资源(测试设备是iPod,1核心。我认为这可能与核心数字无关)?

Could anyone tell me hints on how to handle this kind of scenario? Thanks in advance!

谁能告诉我如何处理这种情况的提示?提前致谢!

Update:

更新:

I have found the root cause. A global variable in App delegate is set to wrong value, therefore UI behavior is weird. I found this by comment all network request method. So this problem is not related with multiple threading. Sorry for the bother.

我找到了根本原因。 App委托中的全局变量设置为错误值,因此UI行为很奇怪。我通过评论所有网络请求方法找到了这个。所以这个问题与多线程无关。抱歉打扰了。

1 个解决方案

#1


0  

I think there needs to be some clarification as to how you are performing the network operations.

我认为需要对如何执行网络操作进行一些澄清。

1st, NSOperatiomQueue deals with NSOperations, so you are presumably wrapping your network code in an NSOperation subclass.

1,NSOperatiomQueue处理NSOperations,因此您可能将网络代码包装在NSOperation子类中。

2nd, are you using NSURLConnections for your networking code?

2,您使用NSURLConnections作为网络代码吗?

3rd, is the blocking part the NSURLConnection or you delegate callback for NSURLConnection?

3,阻塞部分是NSURLConnection还是你委托NSURLConnection的回调?

1 thing to note is that plain ol' NSURLConnections are implemented under the hood multithreaded. The object is placed into your main threads run loop by default (when run from the main thread), but the object is just a wrapper that handles callbacks to the delegate from the lower level networking code (BSD sockets) which happens on another thread.

需要注意的一点是,简单的'NSURLConnections是在多线程引擎下实现的。默认情况下(从主线程运行时)对象被置于主线程运行循环中,但该对象只是一个包装器,它处理来自另一个线程上发生的较低级别网络代码(BSD套接字)的委托的回调。

You really shouldn't be able to block your UI with NSURLConnections on the main thread, unless A) you are blocking the thread with expensive code in the delegate callback methods or B) you are overwhelming your run loop with too many simultaneous URL connections (which is where NSOperationQueue's setMaxConcurrentOperationsCount: comes into play)

你真的不应该在主线程上使用NSURLConnections阻止你的UI,除非A)你在委托回调方法中使用昂贵的代码阻塞线程或者B)你的运行循环压倒了太多的同时URL连接(这是NSOperationQueue的setMaxConcurrentOperationsCount:发挥作用的地方)

#1


0  

I think there needs to be some clarification as to how you are performing the network operations.

我认为需要对如何执行网络操作进行一些澄清。

1st, NSOperatiomQueue deals with NSOperations, so you are presumably wrapping your network code in an NSOperation subclass.

1,NSOperatiomQueue处理NSOperations,因此您可能将网络代码包装在NSOperation子类中。

2nd, are you using NSURLConnections for your networking code?

2,您使用NSURLConnections作为网络代码吗?

3rd, is the blocking part the NSURLConnection or you delegate callback for NSURLConnection?

3,阻塞部分是NSURLConnection还是你委托NSURLConnection的回调?

1 thing to note is that plain ol' NSURLConnections are implemented under the hood multithreaded. The object is placed into your main threads run loop by default (when run from the main thread), but the object is just a wrapper that handles callbacks to the delegate from the lower level networking code (BSD sockets) which happens on another thread.

需要注意的一点是,简单的'NSURLConnections是在多线程引擎下实现的。默认情况下(从主线程运行时)对象被置于主线程运行循环中,但该对象只是一个包装器,它处理来自另一个线程上发生的较低级别网络代码(BSD套接字)的委托的回调。

You really shouldn't be able to block your UI with NSURLConnections on the main thread, unless A) you are blocking the thread with expensive code in the delegate callback methods or B) you are overwhelming your run loop with too many simultaneous URL connections (which is where NSOperationQueue's setMaxConcurrentOperationsCount: comes into play)

你真的不应该在主线程上使用NSURLConnections阻止你的UI,除非A)你在委托回调方法中使用昂贵的代码阻塞线程或者B)你的运行循环压倒了太多的同时URL连接(这是NSOperationQueue的setMaxConcurrentOperationsCount:发挥作用的地方)