ios - Setting an image after retreaving the result of a post request takes 17s -
i making post request server returns string, says if user exist or not nickname, post notification works flawless , prints out returned result in under 1s inside completion handler, takes additional 17 20 set image based on returned result...
here code using:
- (void)textfielddidbeginediting:(uitextfield *)textfield{ // each textfield selected checkl previous text field formating if (textfield == registeremailaddresstextfield) { //check if nickname @ least 6 charactes if (registernicknametextfield.text.length < 6) { nslog(@"emial text field"); [registernicknamecheckmarklabel setimage:[uiimage imagenamed:@"wrong_checkmark.png"]]; }else{ nslog(@"checking nickname"); //check if nickname exists or not nsurl *url = [nsurl urlwithstring:@"myurl"]; //create session custom configuration nsurlsessionconfiguration *sessionconfiguration = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:sessionconfiguration]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] initwithurl:url]; request.httpmethod = @"post"; nserror *error = nil; nsstring* str = [nsstring stringwithformat:@"username=%@", registernicknametextfield.text]; nsdata* jsondata = [str datausingencoding:nsutf8stringencoding]; [request setvalue:[nsstring stringwithformat:@"%d", (int)registernicknametextfield.text.length] forhttpheaderfield:@"content-length"]; if (!error) { nsurlsessionuploadtask *uploadtask = [session uploadtaskwithrequest:request fromdata:jsondata completionhandler:^(nsdata *data,nsurlresponse *response,nserror *error) { nsstring *responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nsstring* result2 = [responsestring stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; nsstring *secondstring = [result2 stringbyreplacingoccurrencesofstring:@" " withstring:@""]; if ([secondstring isequaltostring:@"exist"]) { nslog(@"nickname allready exists"); [registernicknamecheckmarklabel setimage:[uiimage imagenamed:@"wrong_checkmark.png"]]; }else{ [registernicknamecheckmarklabel setimage:[uiimage imagenamed:@"correct_checkmark.png"]]; nslog(@"nickname doesn't exists"); } }]; [uploadtask resume]; } } } }
i tried no success force refresh screen using these code:
dispatch_time_t poptime = dispatch_time(dispatch_time_now, (int64_t)(0.005 * nsec_per_sec)); dispatch_after(poptime, dispatch_get_main_queue(), ^(void) { [self.view setneedsdisplay]; });
so suggested @dan putted on main queue:
dispatch_async(dispatch_get_main_queue(), ^{ if ([secondstring isequaltostring:@"exist"]) { [registernicknamecheckmarklabel setimage:[uiimage imagenamed:@"wrong_checkmark.png"]]; }else{ [registernicknamecheckmarklabel setimage:[uiimage imagenamed:@"correct_checkmark.png"]]; } });
Comments
Post a Comment