如果在数据下载期间交换视图,iPhone应用程序将崩溃

时间:2010-12-06 19:13:35

标签: iphone objective-c uitableview uinavigationcontroller

我有一个iphone应用程序,当用户单击一个uitable中的行时,它会获取行值并从Web下载一些数据以填充下一个视图。但是,如果用户在下载数据时切换回第一个视图,则应用程序崩溃。我想我已经找到了问题,但需要一些帮助来修复它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
BlogRssParser *blogRss = [[BlogRssParser alloc] init];
blogRss.terms = [[selectedObject valueForKey:@"data"] description];

RssFunViewController *rssFun = [[RssFunViewController alloc] initWithNibName:@"RssFunViewController" bundle:nil];

rssFun.rssParser = blogRss;
[blogRss release];
[self.navigationController pushViewController:rssFun animated:YES];
rssFun.navigationItem.title=blogRss.terms;
[rssFun release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

所以它说[self.navigationController pushViewController:rssFun animated:YES];这就是它崩溃的地方,因为一旦它完成下载,这就是下一行代码,如果它没有在正确的屏幕上,它可以推送一个视图,如果这有意义的话!无论如何,谢谢你的建议!

BlogRssParser:

-(BOOL)fetchAndParseRss{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

//To suppress the leak in NSXMLParser
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];

NSString *urlTerm = terms;
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@" " withString:@"+"];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"\t" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"&" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"'" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"-" withString:@""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:@"_" withString:@""];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"xxxxxxxxxxxxx/app.php?s=%@", urlTerm]];  
NSLog(@"%@", url);

BOOL success = NO;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldReportNamespacePrefixes:YES];
[parser setShouldResolveExternalEntities:NO];
success = [parser parse];
[parser release];
[pool drain];
return success;

}

控制台:

    2010-12-06 19:15:09.826 Example[452:207] -[NSCFString processCompleted]: unrecognized selector sent to instance 0x6123d30
2010-12-06 19:15:09.855 Example[452:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString processCompleted]: unrecognized selector sent to instance 0x6123d30'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02664b99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x027b440e objc_exception_throw + 47
    2   CoreFoundation                      0x026666ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x025d62b6 ___forwarding___ + 966
    4   CoreFoundation                      0x025d5e72 _CF_forwarding_prep_0 + 50
    5   Foundation                          0x000423ca __NSThreadPerformPerform + 251
    6   CoreFoundation                      0x02645faf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    7   CoreFoundation                      0x025a439b __CFRunLoopDoSources0 + 571
    8   CoreFoundation                      0x025a3896 __CFRunLoopRun + 470
    9   CoreFoundation                      0x025a3350 CFRunLoopRunSpecific + 208
    10  CoreFoundation                      0x025a3271 CFRunLoopRunInMode + 97
    11  GraphicsServices                    0x02f4300c GSEventRunModal + 217
    12  GraphicsServices                    0x02f430d1 GSEventRun + 115
    13  UIKit                               0x002d1af2 UIApplicationMain + 1160
    14  Example                             0x0000244a main + 84
    15  Example                             0x000023ed start + 53
)
terminate called after throwing an instance of 'NSException'

2 个答案:

答案 0 :(得分:1)

您是否尝试在后台线程中下载XML?这可能会缓解一些问题,因为主线程不会被阻止。在下载XML时,你应该能够推送RssFunViewController。

答案 1 :(得分:1)

unrecognized selector表示您试图向不知道如何处理它的对象发送消息。

例如,假设您有一个班级AlienParser,它有两种方法:landprobe。您创建了一个名为myParser的实例,然后尝试调用[myParser destroyAllHumans]。生成的对象不知道该怎么做,并且会抛出异常。它编译是因为你可以使用Obj-C向任何事件发送任何消息,因为在运行时它可能知道如何处理它,即使编译器无法检测到它。

某处(十六进制是你的线索,它没有显示完整的回溯)你有一些代码调用另一个对象,其中一条消息,它只是普通的不支持。可能值得一提的是,nil的任何消息都不会执行任何操作并返回nil,因此您显然已经在那里发送了一个实际的对象。