ios - How to pass data between sequential Commands? -


i have client wants retrieve data based on combined effort of 2 commands. reason need 2 commands second command relies on data first command. question best way pass data first command second command?

i'm using coordinating object delegate of first , second command - seems messy. resolvable in layer commands housed or require coordinating object?

here's representation of client class:

class viewcontroller: uiviewcontroller, coordinatordelegate {      // ...      @ibaction func didtouchbutton(sender: uibutton) {         self.coordinator.fetchschoolid(firtname: "jane", lastname: "jones")     }      // mark: - coordinatordelegate      func fetchschoolidsucceeded(id id: int) {         self.updateuiwithschoolid(id)     }      func fetchschoolidfailed(error error: nserror) {         self.displayerror(error)     } } 

the coordinator object:

protocol coordinatordelegate {      func fetchschoolidsucceeded(id id: int)     func fetchschoolidfailed(error error: nserror)  }  struct coordinator: fetchschoolinfoactiondelegate, fetchstudentinfoactiondelegate {      let actionfactory: actionfactory     var delegate: coordinatordelegate?      func fetchschoolid(firstname: string, lastname: string) {          let firstaction = self.actionfactory.fetchstudentinfoaction(firstname: firstname, lastname: lastname, delegate: self)          firstaction.execute()     }      // mark: - fetchstudentinfoactiondelegate      func fetchstudentinfosucceeded(studentinfo: studentinfo) {          let secondaction = self.actionfactory.fetchshoolinfoaction(schoolname: studentinfo.schoolname, delegate: self)          secondaction.execute()     }      func fetchstudentinfofailed(error: nserror) {         self.delegate?.fetchschoolidfailed(error: error)     }      // mark: - fetchschoolinfoactiondelegate      func fetchschoolidsucceeded(schoolinfo: schoolinfo) {         self.delegate?.fetchschoolidsucceeded(id: schoolinfo.id)     }      func fetchschoolidfailed(error: nserror) {         self.delegate?.fetchschoolidfailed(error: error)     }  } 

summarily, client (viewcontroller) wants fetch schoolid given firstname , lastname of student. achieve this, 2 commands need executed - fetchstudentinfoaction , fetchschoolinfoaction. second command depends on data first command - in case, fetchschoolinfoaction needs student's schoolname retrieved fetchstudentinfoaction.

this seems messy. if imagine more requests added viewcontroller, coordinator object more , more complex. there better way deal set of sequential commands when second command requires data first? handled object @ command layer instead of coordinator?

the combined action (fetchschoolid) hints @ entity/relation graph become more complex. suggest add completion handler pattern returning result of asynchronous requests.

for example, if execute() function provided completion handler, able specific call in implementation of fetchschoolid.

    let firstaction = self.actionfactory.fetchstudentinfoaction(firstname: firstname, lastname: lastname, delegate: self)      firstaction.execute()      {        (studentinfo) in        let secondaction = self.actionfactory.fetchshoolinfoaction( ....        secondaction.execute ...     } 

this require slight design change in action factory (to store , call completion capture increase flexibility combining , chaining actions.

to implement in action class this:

class fetchstudent:yourbaseactionclass {    var completionhandler:((studentinfo)->())? = nil     // execute allow specification of completion handler    // doesn't require    override func execute(completion:((studentinfo)->())? = nil)    {       completionhandler = completion    }     // assume have in here    // builds studentinfo before returning through delegate    func fetchcomplete()     {       //... code prepared student info       completionhandler?(studentinfo)    }     ... 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -