loops - Use core data entity in a countdown timer -


i have 2 entities in core data create countdown timers. timer has attribute called timername , entity blinds(changed 'times') has attribute called duration.

entities called

timer <---->> blind 

and attributes called

timername <---->> duration 

with relationships called

blinds <---->>timer 

i need place various durations countdown timer 1 @ time. when first duration reaches 0 next duration fetched core data , counted down 0 etc.

i new objective-c , core data know need loop , fetch request don't know start. code examples appreciated. thanks

edit

i have setup fetchrequest in model.m

- (nsfetchedresultscontroller *)frc_newtimer { if (_frc_newtimer) return _frc_newtimer;      // otherwise, create new frc, , set property (and return below) _frc_newtimer = [_cdstack frcwithentitynamed:@"timer"                       withpredicateformat:nil                           predicateobject:nil                           sortdescriptors:@"timername,yes"                     andsectionnamekeypath:nil];  return _frc_newtimer; } 

then in view controller.h

#import <uikit/uikit.h> #import "timer.h" #import "blind.h"  @interface blindtimerviewcontroller : uiviewcontroller <nsfetchedresultscontrollerdelegate> { iboutlet uilabel *lblcountdown; nstimer *countdowntimer; int secondscount; } - (ibaction)starttimer:(id)sender; - (ibaction)resettimer:(id)sender; @property (assign, nonatomic) nsinteger currenttimeindex; @property (nonatomic, strong) model *model; @property (nonatomic, strong) timer *mytimer; @end 

then in view controller.m

@interface blindtimerviewcontroller ()  @end  @implementation blindtimerviewcontroller @synthesize model = _model; 

and

-(void) timerrun  { secondscount = secondscount -1; int minutes = secondscount / 60; int seconds = secondscount - (minutes * 60);  nsstring *timeroutput = [nsstring stringwithformat:@"%2d:%.2d", minutes, seconds]; lblcountdown.text = timeroutput;  //need add label next blind in coredata list , update while in loop......  if (secondscount == 0) {     [countdowntimer invalidate];     countdowntimer = nil;     }  }  -(void) settimer{     // configure , load fetched results controller self.model.frc_newtimer.delegate = self; self.model.frc_newtimer.fetchrequest.predicate = [nspredicate predicatewithformat:@"timername %@", @"sample timer"];  //add code first coredata item in blinds list  secondscount = 240; // need insert coredata blinds here countdowntimer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(timerrun) userinfo:nil repeats:yes]; } 

and buttons (yet sorted) start actions

- (ibaction)starttimer:(id)sender { [self settimer];  }  - (ibaction)resettimer:(id)sender {     [countdowntimer invalidate];     countdowntimer = nil; secondscount = 0; lblcountdown.text = @"00:00";   } 

i'm assuming you're running countdown known timer. in case don't need fetch request have relationship timer set of times, can access directly:

nsset *times = self.mytimer.times; 

we want sort can run durations in order: (you might want check count of times > 0)

nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"duration" ascending:yes]; nsarray *orderedtimes = [times sortedarrayusingdescriptors:@[ sortdescriptor ]]; 

next, we're going need instance variable track are:

@property (assign, nonatomic) nsinteger currenttimeindex; 

with these parts, can manage process, , use nstimer work. when timer fires go time, , sort times, increment index we're using, check index in range, duration , start timer.

i'm going cheeky , if expiring timer nil, means we're starting process scratch (it better take first case out specific method):

- (void)timerfired:(nstimer *)expiringtimer {     [expiringtimer invalidate];      nsinteger index = (expiringtimer != nil ? (self.currenttimeindex + 1) : 0);      nsset *times = self.mytimer.times;      if (times.count < index) {         nssortdescriptor *sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"duration" ascending:yes];         nsarray *orderedtimes = [times sortedarrayusingdescriptors:@[ sortdescriptor ]];          double duration = [[[orderedtimes objectatindex:index] duration] doublevalue];          [nstimer scheduledtimerwithtimeinterval:duration target:self selector:@selector(timerfired:) userinfo:nil repeats:no];     } else {         // deal error     } } 

now can start countdown [self timerfired:nil];

you haven't said you're doing while timers running, change things quite bit (like want display update of time on screen each second)...

if need fetch timer core data db, that's fetch request comes in:

nsmanagedobjectcontext *context = <#managed object context#>; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"timer"]; [fetchrequest setpredicate:[nspredicate predicatewithformat:@"timername %@", @"sample timer"]];  nsarray *timers = [context executefetchrequest:fetchrequest error:nil]; // should add error...  timer *mytimer = nil;  if (timers.count == 1) {     mytimer = [timers lastobject]; } else {    // didn't find timer, agh! } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -