我正在通过同步创建NSOperation和打开NSURLConnections来实现服务器的后端数据加载。
这段代码在模拟器和设备中都工作得很好,直到我添加了一个进度条,并将通知返回到主线程。
- (void)start {
// stop execution if it's cancelled
if([self isCancelled]) {
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
}
// If the operation is not canceled, begin executing the task.
[self willChangeValueForKey:@"isExecuting"];
[NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
}现在,我在同步调用中使EXC_BAD_ACCESS崩溃。
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/index.php", rootURL]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSError *error = nil;
NSURLResponse *response = nil;
receivedData = [[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error] mutableCopy];堆栈跟踪的运行方式如下:
#0 0x3430debc in objc_msgSend ()
#1 0x3136f996 in NSPopAutoreleasePool ()
#2 0x31374160 in -[NSAutoreleasePool release] ()
#3 0x331a0408 in _UIApplicationHandleEvent ()
#4 0x325339c4 in PurpleEventCallback ()
#5 0x3147a52a in CFRunLoopRunSpecific ()
#6 0x31479c1e in CFRunLoopRunInMode ()
#7 0x3314ec08 in -[UIApplication _run] ()
#8 0x3314d230 in UIApplicationMain ()
#9 0x00002ab0 in main (argc=1, argv=0x2ffff504)有了NSZombieEnabled,我得到了
*** -[NSDateComponents release]: message sent to deallocated instance 0x172fd0我知道连接正在建立(查看服务器日志,请求正在进行)。在NSOperation代码中NSURLConnection之前的任何位置都没有分配NSDateComponent对象。
我一直在试图弄清楚我突然对NSURLConnection做错了什么,让它如此讨厌我。
任何帮助都是非常感谢的!
发布于 2009-09-19 02:14:42
我也在开发者论坛上发布了这个问题,并得到了删除并发性的建议。我删除了有问题的行:
[NSThread detachNewThreadSelector:@selector(main ) toTarget:selfwithObject:nil];我也停止了重写start,只重写了main。
NSURLConnection停止崩溃。
发布于 2009-09-18 04:02:33
虽然我不知道你的问题到底是什么,但我知道在使用NSURLConnection时,我也会遇到随机的错误和崩溃。然后我找到了ASIHTTPRequest,再也没有回头。它是在CFNetwork上运行的NSURLConnection的一个替代版本。它非常稳定(我在我的所有项目中都使用它),并且附带了一堆好东西,比如内置的进度条支持和部分下载-恢复。它甚至可以直接写入磁盘以节省内存,这在iPhone中很重要。
https://stackoverflow.com/questions/1442489
复制相似问题