ios - Do I need to completely scrap my application to add Core Data? -
i beginner , when first made application (simple to-do list app) did not know core data. trying implement core data seems if want so, have change application completely. example, created new master detail application core data , compared current application, , none of same.
one confusing part in current table view, have 2 sections objects 2 different arrays. if add core data, have no idea how so. have eliminate arrays , use nsfetchedresultscontroller? also, in application have modal view controllers. in master detail application seems core data works in master view. have no idea how implement core data in modal view controller (which defines values task object). have declare properties managedobjectcontext, managedobject, entitydescription, fetchrequest, etc. again in modal view controller.h?
here of code if care:
tableviewcontroller.m
-(nsmutablearray *)taskarray { if (!taskarray) { taskarray = [nsmutablearray array]; } return taskarray; } -(nsmutablearray *)completedarray { if (!completedarray) { completedarray = [nsmutablearray array]; } return completedarray; } -(ibaction)changesettings:(id)sender{ settingsviewcontroller *svc = [[settingsviewcontroller alloc]initwithcolor:basecolor]; [svc setdelegate:self]; [svc setmodaltransitionstyle:uimodaltransitionstylecrossdissolve]; [self presentviewcontroller:svc animated:yes completion:null]; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ cellsubclasscell *cell = [tableview dequeuereusablecellwithidentifier:@"uitableviewcell"]; if (!cell) cell = [[cellsubclasscell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"uitableviewcell"]; if([indexpath section] == 0){ cell.textlabel.text = [[[self.taskarray objectatindex:[indexpath row]] taskname] uppercasestring]; cell.imageview.image = [uiimage imagenamed:@"unchecked.png"]; cell.imageview.highlightedimage = [uiimage imagenamed:@"uncheckedhighlighted.png"]; [cell setaccessorytype:uitableviewcellaccessorydisclosureindicator]; [cell setbackgroundcolor:[uicolor colorwithred:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; cell.textlabel.textcolor = basecolor; nsstring *detailtext = [[self.taskarray objectatindex:[indexpath row]] timeintervalstring]; cell.detailtextlabel.text = detailtext; [[cell detailtextlabel] setfont:[uifont fontwithname:@"avenir-black" size:12]]; [[cell textlabel] setfont:[uifont fontwithname:@"avenirnext-demibold" size:16]]; [cell.contentview setalpha:1]; } else if ([indexpath section] == 1) { cell.textlabel.text = [[[self.completedarray objectatindex:[indexpath row]] taskname] uppercasestring]; cell.imageview.image = [uiimage imagenamed:@"checked.png"]; [cell setaccessorytype:uitableviewcellaccessorydisclosureindicator]; [cell setbackgroundcolor:[uicolor colorwithred:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; cell.textlabel.textcolor = basecolor; nsstring *detailtext = [[self.completedarray objectatindex:[indexpath row]] timeintervalstring]; cell.detailtextlabel.text = detailtext; [[cell detailtextlabel] setfont:[uifont fontwithname:@"avenir-black" size:12]]; [[cell textlabel] setfont:[uifont fontwithname:@"avenirnext-demibold" size:16]]; [cell.contentview setalpha:0.5]; } uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(handlechecking:)]; //cell.contentview [cell.imageview addgesturerecognizer:tap]; cell.imageview.userinteractionenabled = yes; return cell; } -(void)handlechecking:(uitapgesturerecognizer *)t{ cgpoint taplocation = [t locationinview:self.tableview]; nsindexpath *tappedindexpath = [self.tableview indexpathforrowatpoint:taplocation]; nsindexpath *newindexpath = nil; if (tappedindexpath.section == 0) { nsuinteger newrowindex = self.completedarray.count; [self.completedarray addobject:[self.taskarray objectatindex:tappedindexpath.row]]; [self.taskarray removeobject:[self.taskarray objectatindex:tappedindexpath.row]]; newindexpath = [nsindexpath indexpathforrow:newrowindex insection:1]; } else { nsuinteger newrowindex = self.taskarray.count; [self.taskarray addobject:[self.completedarray objectatindex:tappedindexpath.row]]; [self.completedarray removeobject:[self.completedarray objectatindex:tappedindexpath.row]]; newindexpath = [nsindexpath indexpathforrow:newrowindex insection:0]; } [self.tableview beginupdates]; [self.tableview insertrowsatindexpaths:@[newindexpath] withrowanimation:uitableviewrowanimationautomatic]; [self.tableview deleterowsatindexpaths:@[tappedindexpath] withrowanimation:uitableviewrowanimationautomatic]; [self.tableview endupdates]; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ nsinteger num = 0; if (section == 0) { num = self.taskarray.count; } else { num = self.completedarray.count; } return num; } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview{ return 2; } -(void)buttonpressed:(id)sender{ for(uiview *v in holdviewsarray){ if(button == sender){ [button sethighlighted:yes]; } } } -(uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section{ if (completedarray == nil){ headerview2.hidden = yes; [headerview2 setneedsdisplay]; } else { headerview2.hidden = no; [headerview2 setneedsdisplay]; } switch (section) { case 0: return [holdviewsarray objectatindex:0]; break; case 1: return [holdviewsarray objectatindex:1]; break; } return 0; } - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section{ switch (section) { case 0: return 60.0; break; case 1: return 43.0; break; } return 0; } -(ibaction)addcell:(id)sender{ properties2viewcontroller *pvc = [[properties2viewcontroller alloc]init]; [pvc setdelegate:self]; [self presentviewcontroller:pvc animated:yes completion:null]; [pvc setmodaltransitionstyle:uimodaltransitionstylecrossdissolve]; } -(void)viewwillappear:(bool)animated{ [super viewwillappear:animated]; [[self tableview] reloaddata]; } -(void)properties2viewcontrollerdidenterpropertiessuccesfully:(tasks *)t{ if (![[t taskname] isequal: @""]) { [taskarray addobject:t]; } [self.tableview reloaddata]; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{ tasks *task = [[tasks alloc]init]; if (indexpath.section == 0){ task.taskname = [[self.taskarray objectatindex:[indexpath row]] taskname]; task.timeinterval = [[self.taskarray objectatindex:[indexpath row]] timeinterval]; task.datecreated = [[self.taskarray objectatindex:[indexpath row]] datecreated]; } else if (indexpath.section == 1){ task.taskname = [[self.completedarray objectatindex:[indexpath row]] taskname]; task.timeinterval = [[self.completedarray objectatindex:[indexpath row]] timeinterval]; task.datecreated = [[self.completedarray objectatindex:[indexpath row]] datecreated]; } detailviewcontroller *dvc = [[detailviewcontroller alloc]init]; [dvc settesttask:task]; [[self navigationcontroller] pushviewcontroller:dvc animated:yes]; } -(void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)sourceindexpath toindexpath:(nsindexpath *)destinationindexpath{ if (sourceindexpath.section == 0){ tasks *tasktomove = [taskarray objectatindex:[sourceindexpath row]]; if (sourceindexpath.row > destinationindexpath.row) { [taskarray insertobject:tasktomove atindex:destinationindexpath.row]; [taskarray removeobjectatindex:(sourceindexpath.row + 1)]; } else if (sourceindexpath.row < destinationindexpath.row) { [taskarray insertobject:tasktomove atindex:(destinationindexpath.row + 1)]; [taskarray removeobjectatindex:(sourceindexpath.row)]; } } else if (sourceindexpath.section == 1){ tasks *completedtasktomove = [completedarray objectatindex:[sourceindexpath row]]; if (sourceindexpath.row > destinationindexpath.row) { [completedarray insertobject:completedtasktomove atindex:destinationindexpath.row]; [completedarray removeobjectatindex:(sourceindexpath.row + 1)]; } else if (sourceindexpath.row < destinationindexpath.row) { [completedarray insertobject:completedtasktomove atindex:(destinationindexpath.row + 1)]; [completedarray removeobjectatindex:(sourceindexpath.row)]; } } } -(void)setediting:(bool)editing animated:(bool)animated{ [super setediting:editing animated:animated]; if ([self isediting]){ for(uiview *v in holdviewsarray){ button.hidden = yes; } } else { button.hidden = no; } } -(void)loadview{ [super loadview]; } -(nsindexpath *)tableview:(uitableview *)tableview targetindexpathformovefromrowatindexpath:(nsindexpath *)sourceindexpath toproposedindexpath:(nsindexpath *)proposeddestinationindexpath{ if (proposeddestinationindexpath.section != sourceindexpath.section) { return sourceindexpath; } return proposeddestinationindexpath; } -(void)changebackgroundcolor:(uicolor *)c{ basecolor = c; [self.tableview setseparatorcolor:c]; [self.tableview setbackgroundcolor:c]; button.fillcolor = c; [button setneedsdisplay]; } @end
basically need save objects added user , save time interval specified user since want add timer each object (so if application closes, objects still there). need full core data implementation this? need core data @ all? appreciate guidance since don't know @ point.
---edit--- question. before adding core data had custom delegation send properties modal view controller table view controller. core data still need these? i'm thinking don't there way send properties 1 view controller next core data?
no, don't have scrap application. however, learning curve of core data steep, , reworking existing app might difficult beginner.
essentially, need set what's called core data stack. means have right classes in place use core data.
i suggest in app delegate (or similar place), set nspersistentstorecoordinator
, nspersistentstore
, , nsmanagedobjectcontext
. in core data terminology, persistent store database file. managed object context way access objects inside file: functions scratchpad, can make changes objects , won't written out persistent store until call save:
on managed object context.
have app delegate set objects when app launches. (the core data xcode project templates you.) then, each view controller accesses core data objects needs know managed object context. suggest define property on each such view controller. then, whenever create view controller, pass in managed object context (so app delegate passes root view controller, , view controller passes next one, etc).
one confusing part in current table view, have 2 sections objects 2 different arrays. if add core data, have no idea how so. have eliminate arrays , use nsfetchedresultscontroller?
you wouldn't have to, it's different pattern use if wanted to.
essentially, nsfetchedresultscontroller
convenient way map core data query table view. few clever things, causing table automatically refresh if core data objects change.
the alternative run own query (presumably in viewdidload:
or viewwillappear:
or similar). you'll have array of objects, can feed existing code. nsfetchrequest
making queries, , method executefetchrequest:error:
on nsmanagedobjectcontext
running them.
another question. before adding core data had custom delegation send properties modal view controller table view controller. core data still need these? i'm thinking don't there way send properties 1 view controller next core data?
there number of ways approach that.
if each of view controllers editing core data objects in same database, make each view controller refresh data (i.e. perform fetch again) whenever view appears.
or still use delegate pattern, have delegate method triggering fetch: doesn't pass properties, says "some content has changed, might want reload, ok?".
or use nsfetchedresultscontroller, should pick changes automatically.
Comments
Post a Comment