0

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

实体调用

Timer <---->> Blind

和属性称为

timerName <---->> duration

关系称为

blinds <---->>timer

我需要一次将各种持续时间放入倒数计时器中。当第一个持续时间达到 0 时,从核心数据中获取下一个持续时间,并将其倒数到零,等等。

我对 Objective-C 和核心数据非常陌生,但我知道我需要一个循环并获取请求,但不知道从哪里开始。任何代码示例将不胜感激。谢谢

编辑

我在我的 model.m 中设置了一个 fetchrequest

- (NSFetchedResultsController *)frc_newTimer
{
if (_frc_newTimer) return _frc_newTimer;

    // Otherwise, create a new frc, and set it as the property (and return it below)
_frc_newTimer = [_cdStack frcWithEntityNamed:@"Timer"
                      withPredicateFormat:nil
                          predicateObject:nil
                          sortDescriptors:@"timerName,YES"
                    andSectionNameKeyPath:nil];

return _frc_newTimer;
}

然后在我的视图中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

然后在视图 controller.m

@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";


}
4

1 回答 1

1

我假设您正在为已知的Timer. 在这种情况下,您不需要获取请求,因为您Timer与它的集合有关系Times,我们可以直接访问它:

NSSet *times = self.myTimer.times;

我们想要对其进行排序,以便您可以按某种顺序运行持续时间:(您可能还想检查次数 > 0)

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"duration" ascending:YES];
NSArray *orderedTimes = [times sortedArrayUsingDescriptors:@[ sortDescriptor ]];

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

@property (assign, nonatomic) NSInteger currentTimeIndex;

通过这些部分,您可以管理流程,并使用 anNSTimer来实际完成工作。当计时器触发时,您返回时间,获取并排序时间,增加我们正在使用的索引,检查索引是否在范围内,获取持续时间并启动计时器。

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

- (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
    }
}

现在你可以开始倒计时了[self timerFired:nil];

你还没有说你在计时器运行时在做什么,这可能会改变很多事情(比如你想每秒在屏幕上显示时间的更新)......

如果您需要从您的 Core Data DB 中获取计时器,那么这就是获取请求的来源:

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!
}
于 2013-05-10T16:11:02.730 回答