가장 좋은 방법은 백그라운드 스레드에서 이러한 작업을 관리하는 별도의 개체를 설정하는 것입니다. 그런 다음 귀하의 앱 대리인에서
- (void)applicationWillResignActive:(UIApplication *)application
이 호출 될 때이 특수한 개체를 중지 시키면 모든 것이 동기화되고 필요에 따라 정리됩니다.
그런 경우 : 응용 프로그램이 다시 활성화수록
- (void)applicationDidBecomeActive:(UIApplication *)application
가 호출되는, 다시 그 배경 스레드에서/설문 조사를 조회 할 개체를 신호. NSTimer하고있는 NSURLConnection 또는 다른 더 좋은 방법을 사용
@implementation PollingObject : NSObject
- (id)init
{
if (self = [super init])
{
_interval = 1; // 1 second interval
_cancel = NO; // default to NO
_isPolling = NO; // default to NO
// init your background queue
_pollQueue = dispatch_queue_create("com.yourconame.yourappname.pollQueue", NULL);
}
return self;
}
- (void)heartbeat
{
if (_cancel)
{
// stop the timer
[_timer invalidate];
_isPolling = NO;
return;
}
// Runs the polling method ONCE on a background queue
dispatch_async(_pollQueue, ^{
[self pollingMethod];
});
}
- (void)pollingMethod
{
// Do actual network polling work here...but only run it once. (don't loop)
}
- (void)startPolling
{
_cancel = NO;
if (_isPolling)
{
NSLog(@"Already polling");
return;
}
// schedule the method heartbeat to run every second
_timer = [NSTimer scheduledTimerWithTimeInterval:_interval target:self selector:@selector(heartbeat) userInfo:nil repeats:YES];
}
- (void)stopPolling
{
// we set the flag here and the next second the heartbeat will stop the timer
_cancel = YES;
}
@end
나는 이해할 수 없다. 인터넷이 없다면 어떻게 서버와 통신 할 것입니까? – Desdenova
모든 것이 LAN 안에 있습니다. 그래서 우리는 서버에 로컬로 연결합니다. – Madhu
아, 네 말이 맞아. 내 잘못이야. – Desdenova