core data - coredata- relationship failing to behave properly -
this structure of coredata architecture.
after adding entries 'artists', using them newly added 'album' entries working perfect.
but problem shown in img- 2 & 3, after assigning 'michael jackson' 'insomniac 2010' album & adding same artist 'baby ft ludacris' losing reference album 'insomniac'.
this code save context in albumdetailviewcontroller.h
- (void)entityrecordstableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath forentity:(id)entity { artist *selectedartist = entity; uitableviewcell * cell = [tableview cellforrowatindexpath:indexpath]; [cell setselected:no animated:yes]; if ([self.pickedartists containsobject:selectedartist]) { [self.pickedartists removeobject:selectedartist]; [cell setaccessorytype:uitableviewcellaccessorynone]; editingalbum.artist = self.pickedartists; [self savethecontext:editingalbum.managedobjectcontext]; // nslog(@"%d",self.pickedartists.count); } else { [self.pickedartists addobject:selectedartist]; [cell setaccessorytype:uitableviewcellaccessorycheckmark]; editingalbum.artist = self.pickedartists; [self savethecontext:editingalbum.managedobjectcontext]; // nslog(@"%d",self.pickedartists.count); } }
i think there must problem in managedobjectcontext saving. should this
self.managedobjectcontext = [(appdelegate *)[[uiapplication sharedapplication] delegate] managedobjectcontext]; [self savethecontext:self.managedobjectcontext];
instead of
[self savethecontext:editingalbum.managedobjectcontext];
i tried failing work per expectations.
i have updated code. please check same. problem in relationship between artist & album. must many many.
your relationship between album & artist 1 many. hence, if select artist multiple album override value. please check database same.
code : click here
Comments
Post a Comment