如果在数据下载过程中交换了视图,iPhone应用程序将崩溃 [英] iPhone app crashes if view is swapped during data download

查看:35
本文介绍了如果在数据下载过程中交换了视图,iPhone应用程序将崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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"]描述];RssFunViewController * rssFun = [[RssFunViewController alloc] initWithNibName:@"RssFunViewController" bundle:nil];rssFun.rssParser = blogRss;[blogRss发布];[self.navigationController pushViewController:rssFun动画:是];rssFun.navigationItem.title = blogRss.terms;[rssFun发布];[tableView deselectRowAtIndexPath:indexPath经过动画处理:是]; 

}

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

BlogRssParser:

 -(BOOL)fetchAndParseRss {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = YES;//抑制NSXMLParser中的泄漏[[NSURLCache sharedURLCache] setMemoryCapacity:0];[[NSURLCache sharedURLCache] setDiskCapacity:0];NSString * urlTerm =条款;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成功=否;NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:url];[parser setDelegate:self];[parser setShouldProcessNamespaces:YES];[parser setShouldReportNamespacePrefixes:YES];[parser setShouldResolveExternalEntities:NO];成功= [解析器解析];[解析器发布];[池排水];返回成功; 

}

控制台:

  2010-12-06 19:15:09.826示例[452:207]-[NSCFString processCompleted]:无法识别的选择器已发送到实例0x6123d302010-12-06 19:15:09.855示例[452:207] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSCFString processCompleted]:无法识别的选择器已发送至实例0x6123d30'***第一次抛出调用堆栈:(0 CoreFoundation 0x02664b99 __exceptionPreprocess + 1851个libobjc.A.dylib 0x027b440e objc_exception_throw + 472 CoreFoundation 0x026666ab-[NSObject(NSObject)didNotRecognizeSelector:] + 1873 CoreFoundation 0x025d62b6 ___转发___ + 9664 CoreFoundation 0x025d5e72 _CF_forwarding_prep_0 + 505 Foundation 0x000423ca __NSThreadPerformPerform + 2516 CoreFoundation 0x02645faf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 157 CoreFoundation 0x025a439b __CFRunLoopDoSources0 + 5718 CoreFoundation 0x025a3896 __CFRunLoopRun + 4709 CoreFoundation 0x025a3350 CFRunLoopRunSpecific + 20810 CoreFoundation 0x025a3271 CFRunLoopRunInMode + 9711 GraphicsServices 0x02f4300c GSEventRunModal + 21712图形服务0x02f430d1 GSEventRun + 11513 UIKit 0x002d1af2 UIApplicationMain + 116014示例0x0000244a主+ 8415示例0x000023ed开始+ 53)引发'NSException'实例后调用终止 

解决方案

无法识别的选择器意味着您试图向不知道如何处理它的对象发送消息./p>

例如,假设您有一个类 AlienParser ,它有两个方法: land probe .您创建一个名为 myParser 的实例,然后尝试调用 [myParser destroyAllHumans] .结果对象将不知道该怎么办,并且会引发异常.之所以进行编译,是因为您可以使用Obj-C将任何消息发送至任何对象,因为在运行时,即使编译器无法检测到它,它也可能知道如何处理它.

在某个地方(十六进制是您的提示,它没有显示完整的回溯),您有一些代码调用另一个对象,并带有一条消息,它只是普通的不支持.可能值得一提的是,发送到 nil 的任何消息均不执行任何操作并返回 nil ,因此您显然有一个实际的对象正在向其中发送消息.

I have an iphone app that when the user clicks a row in a uitable, it takes the row value and downloads some data from the web to populate the next view. However if the user switches back to the first view when the data is being downloaded the app crashes. I think i've found the problem but need some help fixing it:

- (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];

}

So where it says [self.navigationController pushViewController:rssFun animated:YES]; this is where it crashes because once it finishes the download this is the next line of code and it can push a view if its not on the right screen if that makes any sense!? Thanks for any advice anyway!

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;

}

Console:

    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'

解决方案

unrecognized selector means that you've attempted to send a message to an object that doesn't know how to handle it.

For example, suppose you had a class AlienParser and it had two methods: land and probe. You create an instance of it called myParser, and then tried to call [myParser destroyAllHumans]. The resulting object wouldn't know what to do, and you'd get an exception thrown. It compiles because you can send any message to anything with Obj-C, because at runtime it may know how to handle it even if the compiler couldn't detect so.

Somewhere (the hex is your clue, it doesn't show a full backtrace) you've got some code calling another object with a message it just plain doesn't support. It's probably worth mentioning that ANY message to nil does nothing and returns nil so you've obviously got an actual object there you are sending messages to.

这篇关于如果在数据下载过程中交换了视图,iPhone应用程序将崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆