2014-11-02 2 views
0

나는 스레드에 매우 익숙해 져 있으므로 은혜를 베풀어주십시오.다른보기 컨트롤러에서 스레드 중지 중

사용자가 인증 된 후 로그인보기 컨트롤러에서 사용자가 로그인하면 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] 
} 
... 
+1

직접 스레드 대신 큐를 사용해보십시오. GCD과 [Concurrency Programming Guide]를 살펴보십시오. – Abizern

+0

NSOperationQueue 또는 NSTimer가 더 좋을 것입니다 (https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html). 그 동안 수면 콤보. –

+0

그리고 로그 아웃하면 대기열이나 타이머를 취소 할 수 있습니다. –

답변

1

나는 이것에 대한 GCD를 사용하는 것이 좋습니다 것입니다.

dispatch_queue_t dq = dispatch_queue_create("bkgrndQueue", NULL); 
dispatch_async(dq, ^{ 
    @autoreleasepool{ 
    while(SEMAPHORE_NAME){ 
     // do stuff in here 
    } 
    } 
} 

한 다음 다른 뷰 컨트롤러에

SEMAPHORE_NAME = NO; 
+0

어디서 SEMAPHORE_NAME을 사용하여 모든 뷰 컨트롤러에서 사용할 수 있는지 확인 하시겠습니까? 유형은 무엇입니까? - 고마워요. – lr100

+1

bool/BOOL/Bool. AppDelegate에서 extern으로 선언해야합니다 ("superglobal"만들기) – TheLivingForce

+0

고마워요. 이 솔루션은 나를 위해 일하고있다! – lr100

관련 문제