How can I wait until NSXMLParserDelegate methods finished?

Mert Karabulut

I want to create object with parsed xml result, but it returns always null. I think it finished before nsxmlparser delegate does not complete.

This is my code.

@interface ParserOperation () <NSXMLParserDelegate, NSURLConnectionDelegate> {
    NSString *soapResults;
    NSXMLParser *xmlParser;
    NSMutableData *responseData;
}

- (void)main {

    NSError *error;
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self];
    if (connection)
        responseData = [NSMutableData data];
    else
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");

    if ([self isCancelled]) {
        return;
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        if (error) {
            self.callbackBlock(NO, error);
            NSLog(@"error %@", error);
        } else {
            self.callbackBlock(YES, self.results);
            NSLog(@"else %@", soapResults);
        }
    });
}

XML parser begins when NSURLConnection delegate methods are completed.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Completed, Received Bytes:%lu",(unsigned long)[responseData length]);

    xmlParser = [[NSXMLParser alloc] initWithData: responseData];
    xmlParser.delegate = self;
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
}

- (void)parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *)attributeDict {

    soapResults = [[NSString alloc] init];
}

- (void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
    soapResults = [soapResults stringByAppendingString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:self.resultName]) {
        if ([soapResults isEqualToString:@""]) {
            NSLog(@"error");
        }
        else {
            NSData *jsonData = [soapResults dataUsingEncoding:NSUTF8StringEncoding];
            NSError *e;
            self.results = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
            NSLog(@"Parsed Object: %@", self.results);
            NSLog(@"soap: %@", soapResults);
        }
    }
}
Rob

There are two issues:

  1. Wain's is absolutely correct, that this is asynchronous network request, so you need to make this a concurrent operation (+1), if it isn't already. Make sure (a) isConcurrent returns YES; (b) implement your own isExecuting and isFinished methods (or define BOOL properties that will synthesize these getters for you); and (c) ensure you manually post the KVN for isFinished and isExecuting when you change the values. All of this is discussed in the Operation Queue chapter of the Concurrency Programming Guide. See the section titled "Configuring Operations for Concurrent Execution".

    By the way, as Wain pointed out, because this is an asynchronous network request, you want to make sure you initiate the call back after the parsing in connectDidFinishLoading.

  2. You also cannot use delegate-based NSURLConnection on an operation (assuming this operation is running on an operation queue) without scheduling this request on a run loop. The easiest solution is to just schedule the connection on the main run loop:

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
    [connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    [connection start];
    

    You can also create your own dedicated thread for NSURLConnection (like AFNetworking does) and schedule your operations on that, but it adds some complexity.

The other approach I've seen people adopt (and it solves both the concurrent operation problem as well as the run loop problem) is to leave this as a non-concurrent operation, but have main not only schedule the operation, but also create its own run loop. I'm not a fan of that technique, but it also works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to wait until all async calls are finished

From Dev

How can I wait until an external process has completed?

From Dev

Wait until function is finished

From Dev

How to make python wait until the previous task is finished?

From Dev

In NodeJs how can I wait until my http get is finished?

From Dev

How to wait until all tasks are finished before running code

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

Selenium: How can I wait until the cursor changes?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until an animation is finished in Swift?

From Dev

How to wait until my batch file is finished

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

Can a for-loop wait until a function within it has finished executing?

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

How can I wait until DownloadManager is complete?

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How can I wait until Asynchronous callbacks are finished before using the data retrieved?

From Dev

How to wait until all NSOperations is finished?

From Dev

How do I wait to delete a file until after the program I started has finished using it?

From Dev

How to make python wait until the previous task is finished?

From Dev

How can I force this jquery to wait until click event has finished?

From Dev

How can I delay an action until after the $apply is finished?

From Dev

How to wait until an http request is finished before moving on?

From Dev

How to make JS wait until protocol execution finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

How to make a python Script wait until download has finished

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

How to wait until canvas has finished re-rendering?

Related Related

  1. 1

    How to wait until all async calls are finished

  2. 2

    How can I wait until an external process has completed?

  3. 3

    Wait until function is finished

  4. 4

    How to make python wait until the previous task is finished?

  5. 5

    In NodeJs how can I wait until my http get is finished?

  6. 6

    How to wait until all tasks are finished before running code

  7. 7

    how to wait until a web request with HttpWebRequest is finished?

  8. 8

    Selenium: How can I wait until the cursor changes?

  9. 9

    How to wait until some jQuery methods are finished?

  10. 10

    How to wait until an animation is finished in Swift?

  11. 11

    How to wait until my batch file is finished

  12. 12

    How to wait until networking by Alamofire has finished?

  13. 13

    How to wait until all tasks finished without blocking UI thread?

  14. 14

    Can a for-loop wait until a function within it has finished executing?

  15. 15

    How do I make an Excel RefreshAll wait to close until finished?

  16. 16

    How can I wait until DownloadManager is complete?

  17. 17

    How to make Gulp wait until dest file is finished?

  18. 18

    How can I wait until Asynchronous callbacks are finished before using the data retrieved?

  19. 19

    How to wait until all NSOperations is finished?

  20. 20

    How do I wait to delete a file until after the program I started has finished using it?

  21. 21

    How to make python wait until the previous task is finished?

  22. 22

    How can I force this jquery to wait until click event has finished?

  23. 23

    How can I delay an action until after the $apply is finished?

  24. 24

    How to wait until an http request is finished before moving on?

  25. 25

    How to make JS wait until protocol execution finished

  26. 26

    How to get jQuery to wait until an animation is finished?

  27. 27

    How to make a python Script wait until download has finished

  28. 28

    Android - How to wait until a SnapshotListener has finished?

  29. 29

    How to wait until canvas has finished re-rendering?

HotTag

Archive