ios - NSURLConnection calling connectionDidFinishLoading before it should? -
i developing ios application fires few nsurl requests. created webservice class implements didreceivedata this:
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { // append new data instance variable declared [_responsedata appenddata:data]; }
where _responsedata nsmutabledata* property. created several subclasses create specific request , implement connectiondidfinishloading. specifically, have registerservice class implements this:
- (void)connectiondidfinishloading:(nsurlconnection *)connection { // request complete , data has been received // can parse stuff in instance variable nserror *error = nil; _parseddata = [nsjsonserialization jsonobjectwithdata:_responsedata options:kniloptions error:&error]; nslog(@"register data: %@",_parseddata); registeranswer serveranswer = [[_parseddata objectforkey:@"success"] integervalue]; if (serveranswer == register_success) { // give data delegate handle [self.delegate successregister]; } else { // serveranswer == register_fail nslog(@"register message: %@",[[_parseddata objectforkey:@"message"] stringvalue]); [self.delegate failedregister]; } }
i have 2 different view controllers use service. 1 of them calls this:
self.personregistering.username = self.txtusername.text; self.personregistering.password = self.txtpassword.text; if (self.personregistering.iscustomer) { // register customer dgregisterservice* myregisterservice = [[dgregisterservice alloc] initwithdelegate:self]; [myregisterservice registerperson:self.personregistering]; } else { // continue picture [self performseguewithidentifier:@"registerpagethreetofour" sender:self]; }
and works perfectly. other 1 (page four) has user take picture before can register. here's code:
- (ibaction)btntakepicture:(id)sender { if ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { // camera available uiimagepickercontroller* mycamera = [[uiimagepickercontroller alloc] init]; mycamera.sourcetype = uiimagepickercontrollersourcetypecamera; mycamera.delegate = self; [self presentviewcontroller:mycamera animated:yes completion:null]; } } #pragma mark - uiimagepicker delegate - (void) imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { [picker dismissviewcontrolleranimated:yes completion:null]; // register person first dgregisterservice* myregisterservice = [[dgregisterservice alloc] initwithdelegate:self]; [myregisterservice registerperson:self.personregistering]; // // send pic // uiimage* picture = [info objectforkey:uiimagepickercontrolleroriginalimage]; // dguploadservice* myuploadservice = [[dguploadservice alloc] initwithdelegate:self]; // [myuploadservice uploadimage:picture forusername:self.personregistering.username]; }
now, second 1 sends request , server processes properly, sending json object:
{"success":1,"message":"account created."}
which expected. however, both nslog statements show "(null)". sometimes, app crashes because objectforkey sent instance doesn't respond it. other times, console show json object after hit "stop" on xcode. rest of times doesn't crash or show object.
since page uses camera, must test on iphone, not simulator. other page shows json object correctly every single time in both simulator , iphone. iphone i'm using 4s ios7.1.1.
if mean nslog
in following lines:
nserror *error = nil; _parseddata = [nsjsonserialization jsonobjectwithdata:_responsedata options:kniloptions error:&error]; nslog(@"register data: %@",_parseddata);
sometimes prints "(null)", caused error in jsonobjectwithdata
call.
i suggest add
nslog(@"received data: %@", _responsedata);
to check fine data, ,
nslog(@"register data: %@ -- (error: %@)",_parseddata, [error localizeddescription]);
to see if error returned.
Comments
Post a Comment