iphone - Core Data Duplicating TableView Cells Upon App Launch -


when relaunch app, cells in table view duplicated. otherwise fine when being added , deleted, etc.

here code:

@implementation routinetableviewcontroller  @synthesize routinetableview; @synthesize eventsarray; @synthesize entered; @synthesize managedobjectcontext;  #pragma mark - view lifecycle  - (void)viewdidload {     if (managedobjectcontext == nil)      {          managedobjectcontext = [(curlappdelegate *)[[uiapplication sharedapplication] delegate] managedobjectcontext];      }      nsfetchrequest *request = [[nsfetchrequest alloc] init];     nsentitydescription *entity = [nsentitydescription entityforname:@"routine" inmanagedobjectcontext:managedobjectcontext];     [request setentity:entity];      nserror *error = nil;     nsmutablearray *mutablefetchresults = [[managedobjectcontext executefetchrequest:request error:&error] mutablecopy];     if (mutablefetchresults == nil) {         // handle error.     }     [self seteventsarray:mutablefetchresults];     [mutablefetchresults release];     [request release];      uibarbuttonitem * addbutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self action:@selector(showprompt)];     [self.navigationitem setleftbarbuttonitem:addbutton];     [addbutton release];      uibarbuttonitem *editbutton = [[uibarbuttonitem alloc]initwithtitle:@"edit" style:uibarbuttonitemstylebordered target:self action:@selector(toggleedit)];     self.navigationitem.rightbarbuttonitem = editbutton;     [editbutton release];      [super viewdidload]; }  - (void)viewdidunload {     self.eventsarray = nil;     [super viewdidunload]; }  -(void)toggleedit {     [self.routinetableview setediting: !self.routinetableview.editing animated:yes];      if (self.routinetableview.editing)         [self.navigationitem.rightbarbuttonitem settitle:@"done"];     else         [self.navigationitem.rightbarbuttonitem settitle:@"edit"]; }  - (void)dealloc {     [managedobjectcontext release];     [eventsarray release];     [entered release];     [super dealloc]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning]; }  #pragma mark - #pragma mark add event  -(void)addevent {         routine *routine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext];      routine.name=entered;      nserror *error = nil;     if (![managedobjectcontext save:&error]) {         // handle error.     }     nslog(@"%@", error);      /*     [eventsarray insertobject:routine atindex:0];      nsindexpath *indexpath = [nsindexpath indexpathforrow:0 insection:0];      [self.routinetableview insertrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];      [self.routinetableview scrolltorowatindexpath:[nsindexpath indexpathforrow:0 insection:0] atscrollposition:uitableviewscrollpositiontop animated:yes];      */      }  -(void)showprompt {     alertprompt *prompt = [alertprompt alloc];     prompt = [prompt initwithtitle:@"add workout day" message:@"\n \n please enter title workout day" delegate:self cancelbuttontitle:@"cancel" okbuttontitle:@"add"];     [prompt show];     [prompt release]; }  - (void)alertview:(uialertview *)alertview willdismisswithbuttonindex:(nsinteger)buttonindex {       if (buttonindex != [alertview cancelbuttonindex])     {         entered = [(alertprompt *)alertview enteredtext];          if(eventsarray && entered)         {             routine *temproutine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext];              temproutine.name = entered;             [eventsarray addobject:temproutine];             [routinetableview reloaddata];             [self addevent];             nslog(@"temproutine.name is: %@",temproutine.name);         }     } }  #pragma mark - table view data source  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     return 1; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return [eventsarray count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cell";      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];      if (cell == nil)     {         cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier] autorelease];     }      routine *temproutine = (routine *)[eventsarray objectatindex:indexpath.row];     cell.textlabel.text = temproutine.name;     cell.accessorytype = uitableviewcellaccessorydisclosureindicator;      return cell; }  // override support conditional editing of table view. - (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  {       if (editingstyle == uitableviewcelleditingstyledelete) {           // delete managed object @ given index path.          nsmanagedobject *eventtodelete = [eventsarray objectatindex:indexpath.row];          [managedobjectcontext deleteobject:eventtodelete];           // update array , table view.          [eventsarray removeobjectatindex:indexpath.row];          [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:yes];           // commit change.          nserror *error = nil;          if (![managedobjectcontext save:&error]) {              // handle error.          }      } 

i think, adding twice....

routine *temproutine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext];              temproutine.name = entered;         ---->    [eventsarray addobject:temproutine];             [routinetableview reloaddata];          ---->   [self addevent]; //comment line in alertviewdelegate 

or can use following code changes..... may have syntax error

- (void)alertview:(uialertview *)alertview willdismisswithbuttonindex:(nsinteger)buttonindex {       if (buttonindex != [alertview cancelbuttonindex])     {         entered = [(alertprompt *)alertview enteredtext];          if(eventsarray && entered)         {            [self addevent];          }     } }   -(void)addevent {         routine *routine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext];      routine.name=entered;      nserror *error = nil;     if (![managedobjectcontext save:&error]) {         // handle error.     }     nslog(@"%@", error);       [eventsarray addobject:routine];      nsindexpath *indexpath = [nsindexpath indexpathforrow:[eventsarray count] insection:0];      [self.routinetableview insertrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];      [self.routinetableview scrolltorowatindexpath:[nsindexpath indexpathforrow:0 insection:0] atscrollposition:uitableviewscrollpositiontop animated:yes];       } 

thanks,


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -