如果在数据下载期间交换视图,iPhone 应用程序会崩溃
我有一个 iPhone 应用程序,当用户单击 uitable 中的一行时,它会获取该行值并从网络下载一些数据以填充下一个视图。但是,如果用户在下载数据时切换回第一个视图,则应用程序会崩溃。我想我已经找到了问题,但需要一些帮助来修复它:
- (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:rssFunAnimated: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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法识别的选择器
意味着您尝试向不知道如何处理该消息的对象发送消息。例如,假设您有一个
AlienParser
类,它有两个方法:land
和probe
。您创建一个名为myParser
的实例,然后尝试调用[myParser destroyAllHumans]
。结果对象不知道要做什么,并且会抛出异常。它可以编译,因为您可以使用 Obj-C 向任何对象发送任何消息,因为在运行时它可能知道如何处理它,即使编译器无法检测到。在某个地方(十六进制是你的线索,它不显示完整的回溯)你有一些代码调用另一个对象,并带有一条它根本不支持的消息。可能值得一提的是,任何发送到
nil
的消息都不执行任何操作并返回nil
,因此您显然已经得到了一个正在向其发送消息的实际对象。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
andprobe
. You create an instance of it calledmyParser
, 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 returnsnil
so you've obviously got an actual object there you are sending messages to.您是否尝试过在后台线程中下载 XML?这可能会缓解一些问题,因为主线程不会被阻塞。您应该能够在下载 XML 时推送 RssFunViewController。
Have you tried downloading the XML in a background thread? This may alleviate some of the issues as the main thread won't be blocked. You should be able to push on the RssFunViewController while the XML is/being downloaded.