NSXMLParser initWithContentsOfURL with dispatch_async( dispatch_get_global_queue(0, 0), ^{ timeout

402 views Asked by At

I am using dispatch queues with NSXMLParser for getting xml data and parsing it.

The code I am using is as below:

dispatch_async( dispatch_get_global_queue(0, 0), ^{

            NSURL *urlNew = [NSURL URLWithString:url_GetData];
            NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:urlNew];
            xmlParser.delegate = self;
            // call the result handler block on the main queue (i.e. main thread)
            dispatch_async( dispatch_get_main_queue(), ^{
                // running synchronously on the main thread now -- call the handler
                [xmlParser parse];


            });
        });

But when server is taking too much time to respond, I need to show an error alert message. I tried to show alert inside the default delegate method, but was not working as expected.

Please help me in setting a 30 seconds timeout for the above code.

If I am not using dispatch queue, the UI is getting freezed till response is received.

Is there any way to achive timeout in the above code snippet.

Thanks.

0

There are 0 answers