Iphone - stop running method?

I have a method [self parseXMLFileAtURL: path]. is there any stop halfway? as the completion of the method.

The reason is that im runs the apache HTTP server on the same computer, and if the server is not running, the application will freeze if this method is called. so I want something like method completion in a certain number of seconds, maybe 5 seconds. and display a warning message.

+3
source share
3 answers

I would have two suggestions ... one, if possible, use NSMutableURLRequest with NSURLConnection to retrieve data; giving you much better control over things like timeout.

NSError * error;
NSURLResponse * response;

NSMutableURLRequest * request = [NSMutableURLRequest
     requestWithURL:[NSURL URLWithString:@"http://..."]
     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
     timeoutInterval:60.0];

// Not sure if you need this, but I frequently do POSTs as well, so whatever:
[request setHTTPMethod:@"GET"];

NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString * xml = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

(NB: , )

, ( ), ... "performSelectorInBackground:" , async. , , ... , .

, , ... , ... NSMutableURLRequest , , .

: http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW2 ... (), ... , -.

+5

, sigaction SIGALRM alarm. , , , Cocoa. , , , .

+2

- C (++), .

0

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


All Articles