开发者

iPhone app crashes if view is swapped during data download

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

RssFunView开发者_StackOverflow社区Controller *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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜