ios - NSURLconnection in a different class -
i'm trying develop app receive data web service displayed in views. in first version of app i've 1 view controller , 1 request web service (by means of nsurlconnection): methods nsurlconnection in viewcontroller , working well.
now need add other views , make other requests thinking best thing apply mvc pattern: in particular have created class (fv_data) put requests , manage nsurlconnections, while in each viewcontroller call method (in fv_data class) needed request.
my problem how return array data web service viewcontroller asked data: in first tests, nsurlconnection correctly started , array (in connectiondidfinishloading method) filled data web service in view controller array empty.
i've read different posts can't understand i'm doing wrong.
this code i've written (i omit code in methods work).
thanks, corrado
fv_data.h
#import <foundation/foundation.h> @interface fv_data: nsobject { nsmutabledata *responsestatistic; nsmutabledata *responsegetstatus; nsurlconnection *connectionstatistic; nsurlconnection *connectiongetstatus; } -(nsarray *)richiedigetstatistic; -(nsarray *)richiedigetstatus; @property (nonatomic, retain) nsarray *arraystatistic; @property (nonatomic, retain) nsarray *arraygetstatus; @end
fv_data.m
#import "fv_data.h" @implementation fv_data -(id)init { self = [super init]; return self; } -(void)richiedigetstatistic{ ... } -(void)richiedigetstatus{ ... } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { ... } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { ... } - (void)connectiondidfinishloading:(nsurlconnection *)connection { if(connection == connectionstatistic){ nsstring *responsestatisticstring = [[nsstring alloc] initwithdata:responsestatistic encoding:nsutf8stringencoding]; self.arraystatistic = [responsestatisticstring componentsseparatedbystring:@","]; } else if(connection == connectiongetstatus){ nsstring *responsegetstatusstring = [[nsstring alloc] initwithdata:responsegetstatus encoding:nsutf8stringencoding]; self.arraygetstatus = [responsegetstatusstring componentsseparatedbystring:@","]; } } @end
fv_live_viewcontroller.h
#import <uikit/uikit.h> #import "fv_data.h" @interface fv_live_viewcontroller : uiviewcontroller { iboutlet uilabel *energia; } @property (nonatomic, retain) fv_data *pvoutputdata; @end
fv_live_viewcontroller.m
#import "fv_live_viewcontroller.h" @implementation fv_live_viewcontroller -(void)viewwillappear:(bool)animated{ self.pvoutputdata = [[fv_data alloc] init]; [self.pvoutputdata richiedigetstatistic]; energia.text = [self.pvoutputdata.arraystatistic objectatindex:0]; } @end
your connectiondidfinishloading
completing download, don't appear doing tell view controller update user interface when data requests done. want that, because loading process takes place asynchronously. example, if initiate process in viewdidload
, loading will, invariably, not until after viewdidload
finished.
you need provide mechanism fv_data
can inform fv_live_viewcontroller
request complete, @ point have view controller update ui new data. 2 typical approaches are:
implement own delegate-protocol pattern, traditional solution sort of problem,
fv_data
can inform view controller request done. see delegation pattern in cocoa core competencies). analogousnsurlconnectiondatadelegate
methods you've implemented infv_data
, in case, rather being mechanismnsurlconnection
informfv_data
of information, mechanismfv_data
informfv_live_viewcontroller
.use completion blocks, more contemporary solution,
fv_live_viewcontroller
can informfv_data
when request done. analogous see in afnetworking, or cocoa methodssendasynchronousrequest
.
but before dive either approach, must i'm not crazy architecture have fv_data
doing 2 separate requests, resulting in 2 sets of class properties , duplication of code in methods. different class structure minimize redundant code (the current code doubles number of opportunities mistakes).
furthermore, i'll go step further , second petro's suggestion consider using proven, existing, third party framework manage these requests, such afnetworking. nsoperation
-based approach afnetworking offers numerous advantages, elegant implementation can complicated (hence suggestion use afnetworking enjoys these advantages while keeping out of weeds of implementation). , unfortunately, nsurlconnection
methods sendasynchronousrequest
convenient, suffer real limitations.
we're happy either way, suggestion.
Comments
Post a Comment