NSThread 在模拟器上工作但在设备上不工作
我有一个连接到某些服务器以获取数据的应用程序。嗯,我正在使用 NSThreads,并且似乎在模拟器中完美工作。一旦我在 iPhone 设备上安装了该应用程序,它就停止工作了。
我不知道这是否是一个常见问题。如果是,什么可以替代 NSThread
?
注意:我尝试了 NSOperations 但仍然遇到同样的问题。
I have an application that connects to some server to get data. Well I'm using NSThreads
and seems work perfectly in simulator. once I installed the application on my iPhone device it stops working.
I don't know if it is a common problem.If yes what can be the alternative to NSThread
?
NOTE: I tried NSOperations
but still get the same problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
NSThread
和NSOperation
都不起作用,那么您的代码存在一些更根本的问题。这两者在设备和模拟器上都同样有效(事实上,否则它们就有点毫无意义)。我强烈建议您更仔细地调查明显的问题。更有可能的是,您遇到某种竞争条件,即使用当前版本操作系统的设备上的特定计时会触发,而使用当前版本 SDK 的模拟器则不会。 iOS 在这方面最常见的错误可能是未能观察到 UIKit 位于仅在主线程上可用的主要部分。但要完全回答这个问题:异步执行工作的主要替代方案是:
dispatch_async
NSObject -performSelectorOnBackground:withObject:
另请注意
NSURLConnection
具有内置的异步操作模式,可向主线程报告,这意味着您实际上不必为自己执行任何显式操作。 Apple 强烈建议您使用它,而不是构建自己的系统,因为它的编写比NSThread
解决方案具有更高的功率和处理器效率。If both
NSThread
andNSOperation
aren't working then you have some more fundamental problem with your code. Both of those work equally well on device and simulator (indeed they'd be a bit pointless otherwise). I highly recommend you investigate the apparent issue more closely. It's much, much more likely that you have some sort of race condition that the particular timing you get on your device with the current version of the OS triggers while your simulator with the current version of the SDK doesn't. The most common error made on iOS in this area is probably failure to observe that UIKit is in the main part usable on the main thread only.But to answer the question exactly as put: the main alternatives for performing work asynchronously are:
dispatch_async
NSObject -performSelectorOnBackground:withObject:
Note also that
NSURLConnection
has an asynchronous mode of operation built in that reports to the main thread, which means you don't actually have to do anything explicit for yourself. Apple strongly recommend you use that rather than building your own system, since it's written to be much more power and processor efficient than anNSThread
solution could be.