What is wrong with this Objective-C code for my iPhone application?

This piece of code:

- (IBAction) getXML {
    goButton.enabled = NO;
    [self performSelectorInBackground:@selector(parseInBackground) withObject:nil];
}

- (void)parseInBackground {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    xmlParser = [[XMLParser alloc] init];
    NSURL *xmlurl = [[NSURL alloc] initWithString:@"http://www.mysite.com/myfile.xml"];
    [xmlParser fetchXMLFromURL:xmlurl];
    [self performSelectorOnMainThread:@selector(didFinishXMLParsing) withObject:nil waitUntilDone:YES];
    [xmlurl release];
    [pool drain];
}

- (void)didFinishXMLParsing {
    goButton.enabled = YES;
}

runs this code:

- (void)fetchXMLFromURL:(NSURL *)xmlurl {
    XMLData = [[NSMutableData alloc] init];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:xmlurl];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection release];
    [request release];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

However, when I go to it in debug, as soon as it gets to ' }' in fetchXMLFromURL, it returns to the line:

[self performSelectorOnMainThread:@selector(didFinishXMLParsing) withObject:nil waitUntilDone:YES];

and the connection to the URL that retrieves the XML never starts. Does anyone know why?


This revised version seems to work correctly, can anyone identify any potential issues?

- (void)fetchXMLFromURL:(NSURL *)xmlurl {
    XMLData = [[NSMutableData alloc] init];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    XMLData = [NSData dataWithContentsOfURL:xmlurl];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    [self startParsingXML];
}

- (void) startParsingXML 
{ 
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:XMLData]; 
    xmlParser.delegate = self; 
    [xmlParser parse]; 
    [xmlParser release];
}

Revised again, hopefully fixed now

- (void)fetchXMLFromURL:(NSURL *)xmlurl {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    XMLData = [[NSData alloc] initWithContentsOfURL:xmlurl];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    [self startParsingXML];
}

- (void) startParsingXML 
{ 
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:XMLData]; 
    xmlParser.delegate = self; 
    [xmlParser parse]; 
    [xmlParser release];
}
+3
source share
2 answers

. NSURLConnection - ( NSURLConnectionDelegate), . +sendSynchronousRequest:returningResponse:error: NSData +dataWithContentsOfURL:options:error:, ( ), . , , .

+4

NSURLConnection , , . NSURLConnection, , - .

+2

Source: https://habr.com/ru/post/1733103/


All Articles