ios - How to update managed object data? -
i have started first core data application. working 1 entity right called 'folder'.
the first view controller displays folders in tableview, can add , reloads data. works fine because uses fetch request populate table.
override func viewwillappear(animated: bool) { var error: nserror? let request = nsfetchrequest(entityname: "folder") request.sortdescriptors = [nssortdescriptor(key: "date", ascending: true)] self.events = moc?.executefetchrequest(request, error: &error) as! [folder] self.uitable.reloaddata() }
however when segueing view controller via table cell pass on selected folder data controller using index path. e.g.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) { if segue.identifier == "showdetails" { let destinationvc = segue.destinationviewcontroller as! folderdetailsviewcontroller let indexpath = uitable.indexpathforselectedrow() let selectedfolder = folders[indexpath!.row] destinationvc.selectedfolder = selectedfolder } }
my second view controller uses data passed first table view display in textfields:
var selectedfolder: folder! foldernamelabel.text = selectedfolder?.title folderdetailslabel.text = selectedfolder?.details folderdatelabel.text = displaydate
i have modal edit/save folder data in modal appearing second controller:
//edit , save event let context = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext //error var error: nserror? //storing data fields selectedfolder!.title = foldername.text selectedfolder!.details = folderdetails.text selectedfolder!.date = folderdate.date context?.save(&error) self.dismissviewcontrolleranimated(true, completion: {});
when dismissing modulate data not updated, have go first controller reload data , segue again.
i think because have no nsfetchrequest
(or nsfetchresultscontroller) recent changes.
what best method reload data of selectedfolder
when make changes in modal ?
you can refresh second view in viewwillappera() if modal view presented in full screen.
override func viewwillappear(animated: bool) { { foldernamelabel.text = selectedfolder?.title folderdetailslabel.text = selectedfolder?.details folderdatelabel.text = displaydate }
Comments
Post a Comment