ios - How to maintain proper encapsulation for singleton created with data -


i have singleton networking class singleton object needs persist throughout app. singleton initialized based on data retrieved web call, right code works, , have following in singleton networking class:

- (void)initializeobjectwithsuccess:(void (^)(bool))success                                  failure:(void (^)(nserror *error))failure { [self.httpclient postpath:[nsstring stringwithformat:@"users/%@/", [cpuser shareduser].name parameters:[self createparameters] success:^(afhttprequestoperation *operation, id responseobject) {     id json = [nsjsonserialization jsonobjectwithdata:responseobject                                               options:nsjsonreadingallowfragments                                                 error:nil];      [[cplist sharedlist] setidentifier:json[@"id"]];     [[cplist sharedlist] setimages:json[@"images"]];     if (success) {         success(yes);     }  } failure:^(afhttprequestoperation *operation, nserror *error) {     failure(error); }]; } 

i don't know how initialize properties need on singleton cplist without setting them within method, know not proper encapsulation because cprequestmanager class should know nothing cplist class

if issue don't want class know name of cplist , detail it's singleton , can access +[cplist sharedinstance] can pass in object conforms protocol. moves knowledge of singleton somewhere else

- (void)initializeobjectwithlist:(id<cplist>)list                          success:(void (^)(bool))success                          failure:(void (^)(nserror *error))failure; {   [self.httpclient postpath:[nsstring stringwithformat:@"users/%@/", [cpuser shareduser].name parameters:[self createparameters] success:^(afhttprequestoperation *operation, id responseobject) {       id json = [nsjsonserialization jsonobjectwithdata:responseobject                                                 options:nsjsonreadingallowfragments                                                   error:nil];         [list setidentifier:json[@"id"]];       [list setimages:json[@"images"]];       if (success) {           success(yes);       }    } failure:^(afhttprequestoperation *operation, nserror *error) {       failure(error);   }]; } 

or remove knowledge there "list" , have method return actual data , caller can set on list

- (void)initializeobjectwithsuccess:(void (^)(nsstring *id, nsarray *images))success                             failure:(void (^)(nserror *error))failure; {   [self.httpclient postpath:[nsstring stringwithformat:@"users/%@/", [cpuser shareduser].name parameters:[self createparameters] success:^(afhttprequestoperation *operation, id responseobject) {       id json = [nsjsonserialization jsonobjectwithdata:responseobject                                                 options:nsjsonreadingallowfragments                                                   error:nil];        if (success) {           success(json[@"id"], json[@"images"]);       }    } failure:^(afhttprequestoperation *operation, nserror *error) {       failure(error);   }]; } 

without further context it's hard suggest structural changes here's 2 potential refactoring might thinking coudld do


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -