Iphone NSThread过早终止

Iphone NSThread过早终止,iphone,objective-c,nsthread,Iphone,Objective C,Nsthread,我有一个应用程序可以通过GData ObjC Mac/iPhone客户端上传到谷歌电子表格。它工作正常。我试图在它自己的线程上获取上载部分,并尝试在新线程上调用upload方法 看: 如果我忘记了使用单独的线程,那么一切都会按照预期的方式工作。我是线程编程新手,所以如果我遗漏了什么,请告诉我 谢谢 我遇到了这个问题,我有了一个解决方案,然而,这个解决方案在工作时让我有些畏缩,但它有点异味。。。看来他们的方法应该更好 我怀疑[helper fetchUserSpreadsheetFeed]中的某个

我有一个应用程序可以通过GData ObjC Mac/iPhone客户端上传到谷歌电子表格。它工作正常。我试图在它自己的线程上获取上载部分,并尝试在新线程上调用upload方法

看:

如果我忘记了使用单独的线程,那么一切都会按照预期的方式工作。我是线程编程新手,所以如果我遗漏了什么,请告诉我


谢谢

我遇到了这个问题,我有了一个解决方案,然而,这个解决方案在工作时让我有些畏缩,但它有点异味。。。看来他们的方法应该更好

我怀疑[helper fetchUserSpreadsheetFeed]中的某个地方您正在使用某种形式的NSURLConnection。如果您使用的是异步http请求(您为回调函数等设置了委托),那么线程可能会在连接有机会调用这些回调函数并以静默方式失败之前终止。下面是我的解决方案,它使线程保持活动状态,直到回调将“finished”变量设置为YES。(我似乎也很难在这些文本框中发布代码,所以如果那些到处跑来跑去编辑东西的天使能帮我解决这个问题,那就太好了!)

}

这个解决方案的问题是,在循环时旋转通常不是好的做法。不过,我不确定runloop业务的性质,它可能是正常睡眠之类的,但我不确定

无论如何,你可以试试看会发生什么


注意:我的“WLUtilities”函数只是NSURLConnection函数的包装器,用于创建异步http请求。您可以尝试的另一个解决方案是简单地使用synchronus请求,但我也不太喜欢这个解决方案,因为异步调用提供了对连接的更细粒度控制。

使用此答案中的技术:


什么是“线程终止,什么也没发生”呢?汤姆,你说得对。gdataobjc客户端异步使用回调选择器。fetchSpreadsheetFeed方法永远无法访问其回调函数。我要试试你的解决方案。谢谢你的提醒
-(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.
}
- (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]];
}