iphone - AFNetworking freezes other actions -
i building chat application repeatedly calls web service using afnetworking
. chat screen polls service new chat messages. related service works fine, ui keeps freezing , none of buttons working.
here code:
- (void)getallincomingmessages { nsurl *url = [nsurl urlwithstring:weatherurl]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest: request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { [self parsejson:(nsdictionary *)json]; [self getallincomingmessages]; } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { [self getallincomingmessages]; uialertview *av = [[uialertview alloc] initwithtitle:@"error " message:[nsstring stringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show]; }]; [operation setauthenticationchallengeblock: ^( nsurlconnection* connection, nsurlauthenticationchallenge* challenge ) { if( [[challenge protectionspace] authenticationmethod] == nsurlauthenticationmethodhttpbasic ) { if( [challenge previousfailurecount] > 0 ) { // avoid many failed authentication attempts lock out user [[challenge sender] cancelauthenticationchallenge:challenge]; } else { [[challenge sender] usecredential:[nsurlcredential credentialwithuser:@"username" password:@"password" persistence:nsurlcredentialpersistenceforsession] forauthenticationchallenge:challenge]; } } else { // authenticate in other ways ntlm if desired or cancel auth this: [[challenge sender] cancelauthenticationchallenge:challenge]; } }]; [operation start]; }
i reload table view each time, ui still freezes. tried using background thread , didn't work either.
i know old question bumped it. fyi afnetworking use dispatched async queue perform connection operation , give json format of nsdata retrieved (as know) in main queue. afnetworking not problem.
my suggestion try perform parsejson: , getallincomingmessages: in separated thread or dispatch async queue , you'll see ui no longer freezing.
something like:
static dispatch_queue_t your_app_queue() { static dispatch_once_t oncetoken; static dispatch_queue_t _myqueue; dispatch_once(&oncetoken, ^{ _myqueue = dispatch_queue_create("com.myapp.queue", dispatch_queue_serial); }); return _myqueue; } afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest: request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { __block myclass = self; dispatch_async(your_app_queue(), ^{ [myclass parsejson:(nsdictionary *)json]; [myclass getallincomingmessages]; }); } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json){ __block myclass = self; dispatch_async(your_app_queue(), ^{ [myclass getallincomingmessages]; dispatch_async(dispatch_get_main_queue(), ^{ uialertview *av = [[uialertview alloc] initwithtitle:@"error " message:[nsstring stringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show]; }); }); }];
or:
afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest: nil success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { [self performselectorinbackground:@selector(parsejson:) withobject:json]; [self performselectorinbackground:@selector(getallincomingmessages) withobject:nil]; } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json){ [self performselectorinbackground:@selector(getallincomingmessages) withobject:nil]; uialertview *av = [[uialertview alloc] initwithtitle:@"error " message:[nsstring stringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show]; }];
and should fine. hope help!
Comments
Post a Comment