4

I am developing an iphone app in which I am extracting RSS feeds and then parsing them.My code is as bellow:

 - (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"in view did appear");

    if ([stories count] == 0) {
        NSString * path = @"http://www.shire.com/shireplc/rss.jsp";

        //[self parseXMLFileAtURL:path];
        //[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
        NSLog(@"internet is %d",[self checkInternet]);
        if([self checkInternet]==1)
        [NSThread detachNewThreadSelector:@selector(parseXMLFileAtURL:) 
                              toTarget:self withObject:path];

}
}

 - (void)parseXMLFileAtURL:(NSString *)URL
  { 
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  stories = [[NSMutableArray alloc] init];

  //you must then convert the path to a proper NSURL or it won't work
  NSURL *xmlURL = [NSURL URLWithString:URL];

  // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
  // this may be necessary only for the toolchain
  rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

  // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];

// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];

[rssParser parse];

 [pool release];

}

Can anyone tell me where I am going wrong? My log is as bellow: log: [Switching to thread 12803] [Switching to thread 12035] 2011-05-10 11:31:30.932 Annual Report[454:490b] found file and started parsing [Switching to thread 14339] 2011-05-10 11:32:04.742 Annual Report[454:640b] found file and started parsing [Switching to thread 13827] [Switching to thread 13827] Program received signal: “EXC_BAD_ACCESS”.

gdb stack trace at 'putpkt: write failed':
0   gdb-arm-apple-darwin                0x0019026b remote_backtrace_self + 54
Yogi
  • 65
  • 1
  • 7
  • 3
    What logs are you getting from the console when you crash? – lxt May 09 '11 at 14:55
  • 1
    Also, run with breakpoints on and tell us the stack trace that you see in the debugger when the application crashes, so that we can identify the cause. I'm betting it's in one of the NSXMLParser delegate callbacks that you haven't shown here, but without more information we can't diagnose this. – Brad Larson May 09 '11 at 16:49
  • Actually I should explain the whole scenario.I am appending all the feeds which I am getting to a string using stringWithFormat.When I observed the allocations at this time,they are exeeding 34 MBs but the app doesn't crash there.When I open the app.the app starts searching for the feeds and when it loads the feeds in web view for that time the allocated memory exceeds 30 MBs.But the app does't crash.Then when I am navigating in the app the parser again enters into parserDidStartDocument: and then application throws a bad access exception which is the only log I get.Aat this time app crash. – Yogi May 10 '11 at 05:30
  • why this bad access and extra memory usage?Why it enters in parserDidStartDocument: for second time?I guess some problem in thread handling.But what? – Yogi May 10 '11 at 05:31
  • I have edited the question to add log of console – Yogi May 10 '11 at 06:10

3 Answers3

0

// you can use the method, which is safer if your have not to update the UserInterface

 [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];

if UserInterface needs also to be updated you can first parse data in background and the update the ui with method.

[self performSelectorOnMainThread:@selector(myUpdateUI) withObject:nil waitUntilDone:YES];
Zubair
  • 5,833
  • 3
  • 27
  • 49
0

I am not sure but i guess the parameter for the method gets released at some point. Can you make sure that the URL is present in the method parseXMLFileAtURL

visakh7
  • 26,380
  • 8
  • 55
  • 69
  • The url is present and I can access it in browser.I am also getting the feeds from it. – Yogi May 10 '11 at 06:23
0

Finally,I used a flag to check if the view is appeared or not(by making flag true in viewdidAppear) and if the view is not appearing,don't run the thread function.That solved the problem!!!

Yogi
  • 65
  • 1
  • 7