0

再会!

我想在一个 xcode 项目中使用 nsthreads,它将调用一个用于网络访问的函数,并检查网络是否存在,所以我需要一个线程,它会在让我们说 1 分钟后执行检查连通性。除非应用程序关闭,否则将继续运行。

[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

开始后台工作

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 个循环

有人可以在这方面帮助我。

谢谢

4

3 回答 3

1

您可以改用 NStimer... 将 Property Repeated 设置为 YES

于 2010-05-04T12:11:57.723 回答
1

当您需要执行冗长的任务但不希望它阻塞应用程序其余部分的执行时,线程特别有用。特别是,您可以使用线程来避免阻塞应用程序的主线程。

要了解更多......从 http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

于 2012-07-18T06:35:51.233 回答
0

我建议结合使用 NSTimer,每 5-10 秒触发一次 + 一个 Reachability 接口,用于检查网络状态。不需要线程。对于使用可达性检查 Apple 的示例。

将此调用放入 viewDidLoad

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

-(void) onTimer:(NSTimer *)theTimer 
{
    //Here perform reachability checks
}
于 2010-05-04T12:49:41.637 回答