简体   繁体   English

IOS4上的NSOperationQueue滞后

[英]NSOperationQueue lag on IOS4

I wrote some sample code in a separate application, that basically queues up items in an NSMutableArray. 我在一个单独的应用程序中编写了一些示例代码,该代码基本上将NSMutableArray中的项目排队。 I then have a method which loops through that array and creates an NSInvocationOperation, puts it in the NSOperationQueue, releases the operation and so forth. 然后,我有一个遍历该数组并创建NSInvocationOperation的方法,将其放入NSOperationQueue中,然后释放该操作,依此类推。 The method then gets called (simply prints out the string that was stored in the array and passed into the operation), does it's job, and sends an NSNotification back to the ViewController. 然后调用该方法(简单地打印出存储在数组中并传递到操作中的字符串),完成其工作,然后将NSNotification发送回ViewController。 The observer method gets hit, but the operations have a lag on them. 观察者方法受到打击,但操作滞后。 For instance, the observer method simply updates a UILabel with how many messages are left in the queue. 例如,观察者方法仅用队列中剩余多少消息来更新UILabel。 It eventually does this, but there seems to be a five second lag in between all of the NSOperations completing and the UI updating. 它最终会完成此操作,但是在所有NSOperations完成和UI更新之间似乎有五秒钟的延迟。 To me it seems like the NSOperationQueue is blocking the main thread. 在我看来,NSOperationQueue正在阻塞主线程。 Is there anyway to get the UI to respond immediately to the notifications? 无论如何,有没有让UI立即响应通知的信息?

One important note is that I have not tested this on the phone yet, just the simulator. 一个重要的注意事项是,我还没有在电话上对此进行过测试,仅是模拟器。 I'm not sure if this makes a difference. 我不确定这是否有所作为。

Sorry in advance. 不好意思 I'm away from my computer and I don't have the code in front of me. 我没有电脑了,我前面没有代码。 Hopefully I explained it well enough. 希望我解释得足够好。 Also I have read the documentation, just haven't found anything that's really answering this specific question for me. 我也已经阅读了文档,只是没有找到真正能回答我这个问题的东西。

The delay is typical of UI updates that are performed on threads other than main. 延迟是在主线程以外的线程上执行的UI更新的典型表现。

To see your update instantly on the UILabel , be sure to invoke whatever method is updating the label's text as follows: 要在UILabel上立即查看您的更新,请确保按以下方式调用任何更新标签text方法:

[self performSelectorOnMainThread:@(myMethodToUpdateLabelWithText:) withObject:text waitUntilDone:NO];

where myMethodToUpdateLabelWithText is a method within your class that sets the label's text value. 其中, myMethodToUpdateLabelWithText是您的类中设置标签文本值的方法。

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

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