簡體   English   中英

NSThread終止太早

[英]NSThread terminating too early

我有一個可通過Mac / iPhone的GData ObjC客戶端上傳到Google Spreadsheets的應用程序。 它按原樣工作正常。 我試圖在自己的線程上獲取上載部分,並且試圖在新線程上調用上載方法。

看:

-(void)establishNewThreadToUpload {
    [NSThread detachNewThreadSelector:@selector(uploadToGoogle) toTarget:self withObject:nil];
}

-(void)uploadToGoogle {
    NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
    //works fine
    [helper setNewServiceWithName:username password:password];
    //works fine
    [helper fetchUserSpreadsheetFeed];
    //inside the helper class, fetchUserSpreadsheet feed calls ANOTHER method, which
    //calls ANOTHER METHOD and so on, until the object is either uploaded or fails
    //However, once the class gets to the end of fetchUserSpreadsheetFeed
    //control is passed back to this method, and
    [pool release];
    //is called.  The thread terminates and nothing ever happens.
}

如果我忘記使用單獨的線程,那么一切都會按預期進行。 我是線程編程的新手,所以如果我缺少什么,請提示我!

謝謝!

我遇到了這個問題,並且有一個解決方案,但是,這種解決方案使我在工作中畏縮不前,但是聞起來有點……似乎他們應該是一個更好的方法。

我懷疑[helper fetchUserSpreadsheetFeed]中的某處您正在使用某種形式的NSURLConnection。 如果您使用的是異步http請求(在其中設置了回調函數等的委托),則線程可能會在連接有機會調用這些回調函數並靜默失敗之前終止。 這是我的解決方案,它使線程保持活動狀態,直到回調將“ finished”變量設置為YES。 (我似乎也很難在這些文本框中發布代碼,因此,如果那些奔波於編輯工作的天使能夠幫助我,那就太好了!)

- (void)fetchFeed {
//NSLog(@"making request");
[WLUtilities makeHttpRequest:self.feedUrlString withHttpHeaders:nil withDelegate:self];

//block this thread so it's still alive when the delegates get called
while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

}

我對此解決方案的問題是,通常不希望在while循環中旋轉。 雖然我不確定runloop業務的性質,但它可能正在適當地睡覺,但我不確定。

無論如何,您可以嘗試一下,看看會發生什么!

注意:我的“ WLUtilities”函數只是NSURLConnection函數的包裝,用於創建異步http請求。 您可能嘗試的另一種解決方案是僅使用syncnus請求,但是我也不喜歡這種解決方案,因為異步調用對連接提供了更精細的控制。

使用此答案中的技術:

如何使用iPhone將照片上傳到服務器?

暫無
暫無

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

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