iPhone xcode 中的 nsthread
再会!
我想在 xcode 项目中使用 nsthreads,该项目将调用一个用于网络访问的函数,并检查网络是否存在,所以我需要一个线程,该线程将在 1 分钟后执行以进行检查连接性。并将继续运行,除非应用程序关闭。
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
startTheBackgroundJob
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:5];
//[self performSelectorInBackground:@selector(checkNet) withObject:nil];
[self performSelectorOnMainThread:@selector(checkNet) withObject:nil waitUntilDone:YES];
[pool release];
它仅在第一次工作,但在其他任何时候都不起作用,我的意思是它只进行了 1 个循环,
有人可以在这方面帮助我。
谢谢
Good Day!
i want to use nsthreads in a project of xcode that will call a function which is for the network access and will check that if the network is there or not, so i need to have a thread which will execute after lets say 1 minutes to check the connectivity. and will continue run unless the app is closed.
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
startTheBackgroundJob
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:5];
//[self performSelectorInBackground:@selector(checkNet) withObject:nil];
[self performSelectorOnMainThread:@selector(checkNet) withObject:nil waitUntilDone:YES];
[pool release];
it works only for the first time but not any other, i mean only 1 loop it makes
can somebody help me in this regard.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 NStimer 代替...将 Property Repeated 设置为 YES
You can use NStimer instead... With Property Repeated set to YES
当您需要执行冗长的任务,但不希望它阻止应用程序其余部分的执行时,线程特别有用。特别是,您可以使用线程来避免阻塞应用程序的主线程。
为了了解更多....
从中找到简单而简短的源代码
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/" xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/
Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application.
To understand more....
find nice simple and short source code from
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/
我建议结合 NSTimer(每 5-10 秒触发一次)+ Reachability 接口(检查网络状态)。不需要线程。要使用 Reachability 检查 Apple 的示例。
将此调用放入 viewDidLoad 中
I'd suggest a combination of NSTimer, that fires each 5-10 seconds + a Reachability interface, which checks for you the network status. No need for a thread. For using the Reachability check Apple's example.
Put this call in viewDidLoad