Loops 在倒计时计时器中使用核心数据实体

Loops 在倒计时计时器中使用核心数据实体,loops,core-data,timer,countdown,Loops,Core Data,Timer,Countdown,我在核心数据中有两个实体来创建倒计时Timer有一个名为timerName的属性,实体Blinds(从“时间”更改)有一个名为duration的属性 实体称为 Timer <---->> Blind 然后在我的视图控制器中 #import <UIKit/UIKit.h> #import "Timer.h" #import "Blind.h" @interface BlindTimerViewController : UIViewController <NS

我在核心数据中有两个实体来创建倒计时
Timer
有一个名为
timerName
的属性,实体
Blinds
(从“时间”更改)有一个名为
duration
的属性

实体称为

Timer <---->> Blind
然后在我的视图控制器中

#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

和按钮(尚未完全排序)来启动操作

- (IBAction)StartTimer:(id)sender
{
[self setTimer];

}

- (IBAction)ResetTimer:(id)sender {
    [countdownTimer invalidate];
    countdownTimer = nil;
secondsCount = 0;
lblCountDown.text = @"00:00";


}

我假设您正在运行已知
计时器的倒计时。在这种情况下,您不需要获取请求,因为您有一个从
计时器到其
时间集的关系,我们可以直接访问它:

NSSet *times = self.myTimer.times;
我们希望对其进行排序,以便您可以按某种顺序运行持续时间: (您可能还希望检查次数是否>0)

接下来,我们需要一个实例变量来跟踪我们的位置:

@property (assign, nonatomic) NSInteger currentTimeIndex;
通过这些部分,您可以管理流程,并使用
NSTimer
实际完成工作。当计时器启动时,您返回时间,获取并排序时间,增加我们正在使用的索引,检查索引是否在范围内,获取持续时间并启动计时器

我会厚颜无耻地说,如果过期计时器为零,这意味着我们正在从头开始这个过程(最好将第一个案例引入一个特定的方法):


感谢上面的代码和解释,我将在我的项目中尝试。是的,我需要每秒钟更新一次显示,并且当Times实体更改显示列表中的下一个时,我还需要更改另一个文本字段。最终,我需要一个声音被激活时,每次达到0。如果我修改了你的代码,我会发布它。你可以使用两个计时器,一个每秒运行一次来更新UI,另一个如上所述运行。或者使用每秒运行的单个计时器。无论哪种方式,您都需要一些实例变量来跟踪UI上应该显示的内容。像“currentTimeIndex”、“currentTimeDuration”和“currentTimeRemaining”。我得到一个错误,说在对象TimerViewController上找不到属性myTimer。“myTimer”只是计时器实例可能被调用的一个示例(因此-我假设您正在运行已知计时器的倒计时。)你需要知道你正在某处运行倒计时的计时器。我显然需要了解更多关于Objective C的信息。我之前已经为计时器添加了数据,计时器名为“Sample timer”,带有一些采样时间。如何引用此代码中存储的计时器?
@interface BlindTimerViewController ()

@end

@implementation BlindTimerViewController
@synthesize model = _model;
-(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 to add a label for the next blind in the coredata list and update it while in a loop......

if (secondsCount == 0) {
    [countdownTimer invalidate];
    countdownTimer = nil;
    }

}

-(void) setTimer{
    // Configure and load the fetched results controller
self.model.frc_newTimer.delegate = self;
self.model.frc_newTimer.fetchRequest.predicate = [NSPredicate predicateWithFormat:@"timerName LIKE %@", @"Sample Timer"];

//add code to get the first coredata item in the blinds list

secondsCount = 240; // i need to insert the CoreData Blinds HERE
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
- (IBAction)StartTimer:(id)sender
{
[self setTimer];

}

- (IBAction)ResetTimer:(id)sender {
    [countdownTimer invalidate];
    countdownTimer = nil;
secondsCount = 0;
lblCountDown.text = @"00:00";


}
NSSet *times = self.myTimer.times;
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"duration" ascending:YES];
NSArray *orderedTimes = [times sortedArrayUsingDescriptors:@[ sortDescriptor ]];
@property (assign, nonatomic) NSInteger currentTimeIndex;
- (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 with the error
    }
}
NSManagedObjectContext *context = <#Managed object context#>;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Timer"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"timerName LIKE %@", @"Sample Timer"]];

NSArray *timers = [context executeFetchRequest:fetchRequest error:nil]; // should really add the error...

Timer *myTimer = nil;

if (timers.count == 1) {
    myTimer = [timers lastObject];
} else {
   // we didn't find the timer, agh!
}