ios - Swift Array Resetting Itself -
i have 2 arrays: pointhistory
, datehistory
. have nstimer
, , when timer runs out, action called. in action appending int
pointhistory
, , current date string
datehistory
. here timer action:
func timeraction() { let steppervalue = int(stepper.value) employee.numberofpoints = steppervalue networking.saveemployee(employee, completion: { (error) -> void in if error != nil { let alert = uialertcontroller(title: "error", message: "there error updating \(self.employee.name)'s points", preferredstyle: .alert) let alertaction = uialertaction(title: "ok", style: .cancel, handler: nil) alert.addaction(alertaction) self.presentviewcontroller(alert, animated: true, completion: nil) } }) var dateformatter = nsdateformatter() dateformatter.dateformat = "mm-dd-yyyy" dateformatter.timezone = nstimezone.localtimezone() var date = nsdate() var datestring = dateformatter.stringfromdate(date) //appending here self.pointhistory.append(self.employee.numberofpoints) self.datehistory.append(datestring) }
however, when view loads up, these 2 arrays reset being empty. here viewdidload
method:
override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.whitecolor() self.title = employee.name editbarbuttonitem = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.edit, target: self, action: selector("edit")) if let font = uifont(name: "avenir", size: 18) { editbarbuttonitem.settitletextattributes([nsfontattributename: font], forstate: uicontrolstate.normal) } self.navigationitem.rightbarbuttonitem = editbarbuttonitem if let font = uifont(name: "avenir", size: 17) { uibarbuttonitem.appearance().settitletextattributes([nsfontattributename: font], forstate: uicontrolstate.normal) } self.addscrollview() self.addprofilepicture() self.adduielements() self.addlinechart() setchart(datehistory, yvals: pointhistory) }
the whole point of datehistory , pointhistory me add line chart app, , of data stored in these arrays. lao why see setchart function. linechart, using ios charts framework. here actual body of setchart function, called in viewdidload:
func setchart(xvals: [string], yvals: [int]) { var dataentries = [chartdataentry]() in 0..<xvals.count { let dataentry = chartdataentry(value: double(yvals[i]), xindex: i) dataentries.append(dataentry) } let linechartdataset = linechartdataset(yvals: dataentries, label: "number of points") let linechartdata = linechartdata(xvals: xvals, dataset: linechartdataset) linechart.data = linechartdata }
finally, supply more information, cause of arrays being reset because implemented viewwillappear function. here is:
override func viewwillappear(animated: bool) { super.viewwillappear(animated) self.refreshview() }
here refreshview
function:
func refreshview() { self.networking.findemployeeforloggedinuser { (array, error) -> void in if error != nil { let alert = uialertcontroller(title: "error", message: "\(error)", preferredstyle: .alert) let alertaction = uialertaction(title: "ok", style: .cancel, handler: nil) alert.addaction(alertaction) self.presentviewcontroller(alert, animated: true, completion: nil) } else { dispatch_async(dispatch_get_main_queue(), { () -> void in self.namelabel.text = self.employee.name self.title = self.employee.name self.rolelabel.text = self.employee.jobdesc self.educationlabel.text = self.employee.education self.birthdaylabel.text = self.employee.birthday self.commentarytextview.text = self.employee.commentary self.emailtextview.text = self.employee.email }) } } self.networking.fetchimageforemployee(employee, completion: { (error, image) -> void in self.imageactind.activityindicatorviewstyle = uiactivityindicatorviewstyle.white self.image.addsubview(self.imageactind) self.imageactind.settranslatesautoresizingmaskintoconstraints(false) self.view.addconstraint(nslayoutconstraint(item: self.imageactind, attribute: .centerx, relatedby: .equal, toitem: self.image, attribute: .centerx, multiplier: 1.0, constant: 0.0)) self.view.addconstraint(nslayoutconstraint(item: self.imageactind, attribute: .centery, relatedby: .equal, toitem: self.image, attribute: .centery, multiplier: 1.0, constant: 0.0)) self.imageactind.startanimating() self.image.alpha = 0.5 if error != nil { dispatch_async(dispatch_get_main_queue(), { () -> void in self.imageactind.stopanimating() self.image.alpha = 1.0 }) let alert = uialertcontroller(title: "error", message: "\(error!)", preferredstyle: .alert) let alertaction = uialertaction(title: "ok", style: .cancel, handler: nil) alert.addaction(alertaction) self.presentviewcontroller(alert, animated: true, completion: nil) } else { dispatch_async(dispatch_get_main_queue(), { () -> void in self.imageactind.stopanimating() self.image.alpha = 1.0 self.image.image = image }) } }) }
i don't know why these arrays being reset being empty. doing wrong?
[this assumes code running inside single uiviewcontroller
instance.]
i think misunderstand aspects of uiviewcontroller
lifecycle. viewdidload()
called once, during app's startup sequence, when none of ui visible, no components have been laid out, , no timers have been given chance start. unless you're loading data in persistent store (disk; database; etc.) @ point, arrays have values set overriding constructor (if have one) or otherwise have values set class-level declaration initializers.
when start nstimer
, , when expect go off?
are using println()
s or debugging breakpoints teach true sequence of events?
i have add you're doing ui code off main thread, big no-no. presentation of alerts must follow same dispatch
pattern non-error ui paths.
Comments
Post a Comment