나는 스레드에 매우 익숙해 져 있으므로 은혜를 베풀어주십시오.다른보기 컨트롤러에서 스레드 중지 중
사용자가 인증 된 후 로그인보기 컨트롤러에서 사용자가 로그인하면 30 초마다 사용자 위치 정보를 얻는 스레드를 실행 한 다음 (사용자가 로그인 할 때만이 작업을 수행하려는 경우) 스레드를 실행하여 응용 프로그램의 주요 정보를 표시하는 응용 프로그램입니다.
사용자가 로그 아웃하면 생성 된 스레드를 취소하고 30 초마다 위치 정보를 수집하려고합니다.
어떻게하면됩니까?
맞습니까? 그렇지 않다면 코드 예제와 설명이 기뻐합니다
고마워요 !!!!
loggin에보기 컨트롤러
...
- (IBAction)loginButton:(id)sender {
NSInteger success = 0;
//Check to see if the username or password texfields are empty or email field is in wrong format
if([self validFields]){
//Try to login user
success = [self loginUser];
}
//If successful, go to the MainView
if (success) {
//Start getting users Geolocation in a thread
[NSThread detachNewThreadSelector:@selector(startGeolocation:) toTarget:self withObject:nil];
//Go to Main view controller
[self performSegueWithIdentifier:@"loginSuccessSegue" sender:self];
}
else
{
//Reset password text field
self.passwordTextField.text = @"";
}
}
...
//Thread to get Geolocation every 30 seconds
-(void)startGeolocation:(id)param{
self.geoLocation = [[GeoLocation alloc] init];
while(1)
{
//****************START GEOLOCATION*******************************//
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.databaseLock lock];
NSLog(@"Geolocation:(%f,%f)", [self.geoLocation getLatitude], [self.geoLocation getLongitude]);
sleep(30);
[appDelegate.databaseLock unlock];
}
}
홈페이지보기 컨트롤러
...
//When the Logout Button in MenuView is pressed this method will be called
- (void)logoutButton{
//Cancel the geolocation tread
//????????????????????????????
//Log the user out
[self logoutUser]
}
...
직접 스레드 대신 큐를 사용해보십시오. GCD과 [Concurrency Programming Guide]를 살펴보십시오. – Abizern
NSOperationQueue 또는 NSTimer가 더 좋을 것입니다 (https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html). 그 동안 수면 콤보. –
그리고 로그 아웃하면 대기열이나 타이머를 취소 할 수 있습니다. –