我有一个包含图像的 UItableView Controller 。我还有一个刷新按钮,用于以 JSON 格式从服务器重新检索数据并再次填充 UITableView。
但是,我面临一个问题,当我单击刷新按钮时,应用程序有时会崩溃(不稳定的行为)。
我的崩溃截图如下
从这里看来,错误似乎来自合成属性,我的假设是错误来自尝试访问属性 aConnection
我检查了左侧面板上的线程,下面的屏幕截图似乎表明当我尝试再次设置 aConnection 时,崩溃发生在某个地方。
我不确定我是否在这里发布了足够的信息,但是任何有关我如何继续的建议将不胜感激
最佳答案
尝试使用此方法。
- (void)getData
{
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
NSString *urlGen = [[NSString alloc] initWithFormat:@"http://myurlforjson"];
NSLog(@"%@",urlGen);
NSURL *url = [NSURL URLWithString:urlGen]; // Modify this to match your url.
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding]; NSError *error = nil;
// I am using specific JSON Libraries to parse json data
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
self.storageDictionary = [dict objectForKey:@"JSONItemNAME"];
[self parseNumberofStores:self.listData];
[self.listData retain];
}
[urlGen release];
[jsonreturn release];
[self.table reloadData];
if ([self.listData count] <= 0 )
{
UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"Oops !" message:@"Sorry ! No Items Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[al show];
[al release];
//[[self navigationController] popViewControllerAnimated:YES];
}
if ([activity isAnimating]) {
[activity stopAnimating];
}
[tableview reloadData];
[pool release];
}
希望这能解决您的问题。
您可能需要从某些点击或加载事件中调用此方法,请尝试使用此方法
[NSThread detachNewThreadSelector: @selector(getData) toTarget:self withObject:nil];
关于objective-c - Objective-C : App Crashes when setting up connection (during refresh),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302056/