簡體   English   中英

線程在模擬器中有效,但在 iPhone 上無效

[英]Threading works in simulator but not on iPhone

我正在嘗試在進行后台處理時顯示 UIActivityIndicatorView 。 下面的簡化代碼在我在模擬器中嘗試時有效(顯示警報)..但是當我從 Xcode 將它下載到我的手機時,后台線程似乎根本沒有被調用。 (警報永遠不會顯示)
有任何想法嗎?

 -(void)viewDidLoad {   
    [self performSelectorInBackground:@selector(runInAnotherThread) withObject:nil];

}

-(void) runInAnotherThread {
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  int i;
    for(i=0;i < 1000 ;i ++){
        NSLog(@"INDEX = %d", i);
    }

    [self performSelectorOnMainThread : @ selector(backToMainThread ) withObject:nil waitUntilDone:NO]; 
    [ pool release ];
}

-(void) backToMainThread {

    UIAlertView *completeAlert = [[UIAlertView alloc] 
                     initWithTitle:@"Back to main "
                message: @"Success" 
                delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil];
    [completeAlert show];
    [completeAlert release];    
}

你試過清理你的構建嗎? 我剛剛在我的設備和模擬器上運行了您的代碼,它在兩種情況下都按預期工作

使用 NSOperation 而不是原始線程操作。 它為您抽象了各種東西(優先級,自動釋放池等......)。 ? 您可以簡單地向您的 NSOperation 子類添加某種委托,以便在需要時獲得回調。

感謝您這么快回復!

事實證明,這個問題根本不在這個代碼片段中。 我根據鑰匙串中的值執行此代碼。 雖然我的模擬器的鑰匙串有這個值,但我的測試 iphone 沒有這個值。

給大家添麻煩了,真傻。 但是跟進 nduplessis 的回復幫助我縮小了問題的范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM