objective c - NSOperationQueue addOperationWithBlock with return in iOS -


i have written method -(void) getstatus:(nsstring*) url return type,

-(nsstring) getstatus:(nsstring*) statusurl {    nsurl *urlobj = [nsurl urlwithstring:[statusurl     stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];    nserror *err;     nsstring *response = [nsstring stringwithcontentsofurl: urlobj encoding:     nsutf8stringencoding error:&err];     return response; } 

but stringwithcontentsofurl performing operation in main thread, while calling method application struck second.

so need perform stringwithcontentsofurl function in background thread , after getting response want return response.

my current code:

-(nsstring) getstatus:(nsstring*) statusurl {    nsurl *urlobj = [nsurl urlwithstring:[statusurl     stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];    nserror *err;     nsoperationqueue *rbqueue = [[[nsoperationqueue alloc] init] autorelease];    [rbqueue addoperationwithblock:^{        nsstring *response = [nsstring stringwithcontentsofurl: urlobj        encoding: nsutf8stringencoding error:&err];        [[nsoperationqueue mainqueue] addoperationwithblock:^{         return response;       }];    }];     return @""; } 

but me receiving empty string @"", can not receive response got server. there way same task..

have noticed there should changes approach complete task? hardly can use getters way because of nature of asynchronous methods. or getters block main thread in turn.

to avoid recommend use nsnotification update ui after complete server request in background thread;

or change getter method definition pass result background thread main thread, asynchronously:

- (void)getstatusasychronously:(nsstring *)statusurl withcompletionblock:(void(^)(nsstring *result))completionblock; 

or consider subclassing nsoperation object server request. nsoperation subclasses handy nsoperationqueue instances , provide more useful features cancellation of operation.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -