ios - Core Data Not Saving To Table View -
so first time working core data, , far hasn't been best experience. application far consists of 2 uitableview controllers , single viewcontroller. app asks user enter name of list on uialert , saves core data, , name of list put first tableview. user clicks on name of list , pushes them contents on list, empty because hasn't been populated yet. user populates tableview, pushes single view controller enter info , hit save. problem doesn't save when hit save, goes last uitableview controller , nothing there. thats first problem, second pass data between views when user clicks on list pushes second uitableview controller , says name of list @ top. i'm getting confused core data stuff , relationships if me out appreciate it. i'll include code , data model.
data model
first views .m (view lists lists)
- (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate respondstoselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; // fetch lists persistent data store nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"list"]; self.lists = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; [self.tableview reloaddata]; } - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return self.lists.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // configure cell... nsmanagedobject *list = [self.lists objectatindex:indexpath.row]; [cell.textlabel settext:[list valueforkey:@"name"]]; return cell; } -(ibaction)add:(id)sender { uialertview *alert = [[uialertview alloc] initwithtitle:@"add list" message:@"create new wish list" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"save", nil]; [alert setalertviewstyle:uialertviewstyleplaintextinput]; [alert settag:2]; [alert show]; alert.delegate = self; } - (void) alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex { if (buttonindex != 0 && alertview.tag == 2) { uitextfield *tf = [alertview textfieldatindex:0]; nsmanagedobjectcontext *context = [self managedobjectcontext]; // create new managed object nsmanagedobject *newlist = [nsentitydescription insertnewobjectforentityforname:@"list" inmanagedobjectcontext:context]; [newlist setvalue:tf.text forkey:@"name"]; nserror *error = nil; // save object persistent store if (![context save:&error]) { nslog(@"can't save! %@ %@", error, [error localizeddescription]); } [self dismissviewcontrolleranimated:yes completion:nil]; } nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"list"]; self.lists = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; [self.tableview reloaddata]; } - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsmanagedobjectcontext *context = [self managedobjectcontext]; if (editingstyle == uitableviewcelleditingstyledelete) { // delete object database [context deleteobject:[self.lists objectatindex:indexpath.row]]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't delete! %@ %@", error, [error localizeddescription]); return; } // remove list table view [self.lists removeobjectatindex:indexpath.row]; [self.items removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } @end
second view's .m (view displays items in list)
- (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate respondstoselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; // fetch lists persistent data store nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"item"]; self.lists = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; [self.tableview reloaddata]; } - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return self.items.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // configure cell... nsmanagedobject *item = [self.items objectatindex:indexpath.row]; [cell.textlabel settext:[item valueforkey:@"list"]]; return cell; } - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsmanagedobjectcontext *context = [self managedobjectcontext]; if (editingstyle == uitableviewcelleditingstyledelete) { // delete object database [context deleteobject:[self.items objectatindex:indexpath.row]]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't delete! %@ %@", error, [error localizeddescription]); return; } // remove device table view [self.items removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } #pragma mark - table view delegate - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. /* <#detailviewcontroller#> *detailviewcontroller = [[<#detailviewcontroller#> alloc] initwithnibname:@"<#nib name#>" bundle:nil]; // ... // pass selected object new view controller. [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; */ } #pragma mark - segue - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([[segue identifier] isequaltostring:@"showdetails"]) { nsmanagedobject *selecteditem = [self.items objectatindex:[[self.tableview indexpathforselectedrow] row]]; addviewcontroller *destviewcontroller = segue.destinationviewcontroller; destviewcontroller.items = selecteditem; } } @end
and last view (view adds items list)
- (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate respondstoselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)cancel:(id)sender { [self dismissviewcontrolleranimated:yes completion:nil]; } - (ibaction)save:(id)sender { nsmanagedobjectcontext *context = [self managedobjectcontext]; // create new managed object nsmanagedobject *newitem = [nsentitydescription insertnewobjectforentityforname:@"item" inmanagedobjectcontext:context]; [newitem setvalue:self.lists forkey:@"list"]; [newitem setvalue:self.lists forkey:@"list"]; [newitem setvalue:self.lists forkey:@"list"]; nserror *error = nil; // save object persistent store if (![context save:&error]) { nslog(@"can't save! %@ %@", error, [error localizeddescription]); } [self dismissviewcontrolleranimated:yes completion:nil]; } @end
in model view allows me create new item:
[newitem setvalue:self.name.text forkey:@"itemname"]; [newitem setvalue:self.price.text forkey:@"price"]; [newitem setvalue:self.desc.text forkey:@"description"];
error:
self=(addviewcontroller *)0x8d22e90 newitem=(item_item_ *)0x8a636b0
it's not clear me how of these controllers wired together. handful of suggestions consider when working core data:
there variety of ways deal managed object context you'll use on main queue. many use dependency injection pass context along controller hierarchy, rather ask application delegate in each controller.
you can simplify life considerably subclassing
nsmanagedobject
don't have use kvc access properties on entities. xcode can you; mogenerator offers more.nsfetchedresultscontroller
can simplify working core data , table views; adds own set of quirks.
that said, can't tell whether of related problems or help; put sample app mirrors part of you're trying do. (i think...) leaves out lot of logic collecting data user etc. can @ how might consider putting core data app such this.
see this repository on github.
Comments
Post a Comment