NSThread 在模拟器上工作但在设备上不工作

发布于 2024-12-03 07:12:49 字数 192 浏览 2 评论 0原文

我有一个连接到某些服务器以获取数据的应用程序。嗯,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2024-12-10 07:12:49

如果 NSThreadNSOperation 都不起作用,那么您的代码存在一些更根本的问题。这两者在设备和模拟器上都同样有效(事实上,否则它们就有点毫无意义)。我强烈建议您更仔细地调查明显的问题。更有可能的是,您遇到某种竞争条件,即使用当前版本操作系统的设备上的特定计时会触发,而使用当前版本 SDK 的模拟器则不会。 iOS 在这方面最常见的错误可能是未能观察到 UIKit 位于仅在主线程上可用的主要部分。

但要完全回答这个问题:异步执行工作的主要替代方案是:

另请注意NSURLConnection 具有内置的异步操作模式,可向主线程报告,这意味着您实际上不必为自己执行任何显式操作。 Apple 强烈建议您使用它,而不是构建自己的系统,因为它的编写比 NSThread 解决方案具有更高的功率和处理器效率。

If both NSThread and NSOperation 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:

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 an NSThread solution could be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文