iphone - confusion with implementing core data -


i beginner , trying move app core data. following big nerd ranch 3rd edition book, in book have store class holds items in item class. app different. have task class , tasks displayed task arrays declared in tableview controller, , if tap on task details come in detail view controller. thing is, book says need create nsmanagedobjectcontext, nsmanagedobjectmodel, , nspersistentstorecoordinator, in store. declare these in app? tableview controller or detail view controller? here code:

tasks.h

#import <foundation/foundation.h> #import <coredata/coredata.h>  @interface tasks : nsmanagedobject  @property (nonatomic) nsdatecomponents *conversioninfo; @property (nonatomic) nstimeinterval datecreated; @property (nonatomic) double orderingvalue; @property (nonatomic, retain) nsstring * taskname; @property (nonatomic) double timeinterval; @property (nonatomic, retain) nsstring * timeintervalstring; @property (nonatomic, retain) nsmanagedobject *assettype;  @end 

tasks.m

@implementation tasks  @dynamic conversioninfo; @dynamic datecreated; @dynamic orderingvalue; @dynamic taskname; @dynamic timeinterval; @dynamic timeintervalstring; @dynamic assettype;  -(void)awakefromfetch{     [super awakefromfetch]; } -(void)awakefrominsert{     [super awakefrominsert];     nstimeinterval t = [[nsdate date] timeintervalsincereferencedate];     [self setdatecreated:t]; } -(nsstring *)timeintervalstring{     nscalendar *syscalendar = [nscalendar currentcalendar];     nsdate *date = [nsdate date];     nsdate *date1 = [nsdate datewithtimeinterval:self.timeinterval sincedate:date];     unsigned int unitflags = nshourcalendarunit | nsminutecalendarunit;     self.conversioninfo = [syscalendar components:unitflags fromdate:date  todate:date1  options:0];     if ([self.conversioninfo hour] == 0){         if ([self.conversioninfo minute] == 1) {             self.timeintervalstring = [nsstring stringwithformat:@"%d min", [self.conversioninfo minute]];         } else {             self.timeintervalstring = [nsstring stringwithformat:@"%d mins", [self.conversioninfo minute]];         }     } else if ([self.conversioninfo hour] == 1) {         if ([self.conversioninfo minute] == 0){             self.timeintervalstring = [nsstring stringwithformat:@"%d hr", [self.conversioninfo hour]];         } else if ([self.conversioninfo minute] == 1) {             self.timeintervalstring = [nsstring stringwithformat:@"%d hr %d min", [self.conversioninfo hour], [self.conversioninfo minute]];         } else {             self.timeintervalstring = [nsstring stringwithformat:@"%d hr %d mins", [self.conversioninfo hour], [self.conversioninfo minute]];         }     } else {         if ([self.conversioninfo minute] == 0) {             self.timeintervalstring = [nsstring stringwithformat:@"%d hrs ", [self.conversioninfo hour]];         } else if ([self.conversioninfo minute] == 1){             self.timeintervalstring = [nsstring stringwithformat:@"%d hrs %d min", [self.conversioninfo hour], [self.conversioninfo minute]];         } else {             self.timeintervalstring = [nsstring stringwithformat:@"%d hrs %d mins", [self.conversioninfo hour], [self.conversioninfo minute]];         }     }     return self.timeintervalstring; } @end 

tableviewcontroller.m

-(nsmutablearray *)taskarray {     if (!taskarray) {         taskarray = [nsmutablearray array];     }     return taskarray; } -(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)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]; } 

detail view controller.m

@interface detailviewcontroller ()  @end @implementation detailviewcontroller @synthesize testtask,timer,timerlabel, date1, alertview, components, timeremaining; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil {     self = [super initwithnibname:nibnameornil bundle:nibbundleornil];     if (self) {       }     return self; }  - (void)viewdidload {     [super viewdidload];     [_timeleft setfont:[uifont fontwithname:@"bebasneue" size:25]];    } -(ibaction)starttimer:(id)sender{     [sender sethidden:yes];     [pausebutton sethidden:no];     [continuebutton sethidden:no];         gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];         self.date1 = [nsdate datewithtimeinterval:[testtask timeinterval] sincedate:[nsdate date]];          timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(timeraction:) userinfo:nil repeats:yes];     [timer fire];  } -(void)viewwillappear:(bool)animated{     [super viewwillappear:animated];      [timerlabel setfont:[uifont fontwithname:@"bebasneue" size:60]];     [[self navigationitem] settitle:[testtask taskname]];     if (startbutton.hidden == no){         [pausebutton sethidden:yes];         [continuebutton sethidden:yes];     } else {         [pausebutton sethidden:no];         [continuebutton sethidden:no];     }       timeremaining = [nsstring stringwithformat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]];     timerlabel.text = timeremaining;     [timerlabel setneedsdisplay];     [self.view setneedsdisplay]; } -(void)viewwilldisappear:(bool)animated{     [super viewwilldisappear:animated];      [testtask settaskname:[testtask taskname]];     [testtask settimeinterval:[testtask timeinterval]];  } -(void)timeraction:(nstimer *)t{      nsdate *now = [nsdate date];     components = [gregoriancalendar components:nshourcalendarunit|nsminutecalendarunit|nssecondcalendarunit  fromdate:now todate:self.date1 options:0];      timeremaining = nil;     if([now compare:self.date1] == nsorderedascending){         timeremaining = [nsstring stringwithformat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]];         nslog(@"works %@", timeremaining);     } else {         timeremaining = [nsstring stringwithformat:@"00:00:00"];         [self.timer invalidate];         self.timer = nil;         if (self.alertview == null){        self.alertview = [[uialertview alloc]initwithtitle:[testtask taskname] message:@"time up!" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];         [alertview performselectoronmainthread:@selector(show) withobject:nil waituntildone:yes];                nslog(@"ended");         }     }    timerlabel.text = timeremaining;     [timerlabel setneedsdisplay];     [self.view setneedsdisplay]; } - (void)alertview:(uialertview *)alertview diddismisswithbuttonindex:(nsinteger)buttonindex{     self.alertview = null; } 

you should declare nsmanagedobjectcontext, nsmanagedobjectmodel, , nspersistentstorecoordinator in appdelgate.h file.

all these 3 objects required allocated menory once in application.
need nsmanagedobjectcontext throughout app save/ edit/retrieve managed objects.

my suggestion: create new project, choose master- detail application template. check use core data option.

you template code regarding core data in appdelegate.h , appdelegate.m file of new application can copy/paste in application.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -