如何设置一个以绝对术语表示未来时间的变量Objective-C [英] How to set a variable that represents a time in the future in absolute terms Objective-C

查看:58
本文介绍了如何设置一个以绝对术语表示未来时间的变量Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动机:我正在开发一个应用程序,该应用程序使多个(客户端)电话从(服务器)电话读取音频数据.他们的想法是,他们必须同时全部同时播放精确的歌曲.

Motivation: I'm working on an app that makes several (client) phones read audio data from a (server) phone. The idea is that they must all play the song at the exact same time together.

前提::我必须找出一种方法,使所有电话都在某个绝对绝对的时间戳上启动(即,它与任何一部电话的使用设置时钟无关,等等).基于一些研究,我认为最好的方法是使用CFAbsoluteTimeGetCurrent();服务器与每部电话进行通信所需要的延迟时间(b/c GKSession显然是串行执行,而不是并行执行),将该延迟添加到当前时间,然后让每部电话从该开始时间开始播放歌曲.该开始时间必须是绝对的,它不能是相对的.

Premise: I must figure out a way to make all phones start at a certain time stamp that is absolute (ie it's not relative to the use set clock of either phone etc..).. based on some research I figured the best way to do this is to use CFAbsoluteTimeGetCurrent(); The idea here is that I get the latency it takes the server to communicate with each phone (b/c GKSession is done serially not in parallel apparently), add that latency to the current time, and then have every phone play the song starting at that start time. This start time must be absolute, it cannot be relative.

问题:我如何用数字表示将来的某个时间,以后可以用来构造

Question: How can I represent a time in the future in a number, that can be later on used to construct a CFDateRef. (The reason it has to be expressed as a number is that I must be able to send it in a packet..)

示例:这是一些代码,解释了我要实现的目标:

Example: This is a bit of code that explains what I'm trying to achieve:

-(void)viewDidLoad
{
    _timer = [Timer new];
    [_timer setCurrentTimeAsReferencepoint];

    [self performSelector:@selector(performAction) withObject:NULL afterDelay:5];
}


-(void)performAction
{
    double diff = [_timer getTimeElapsedinAbsTime];
    double timeInFuture = diff + [Timer getCurTime];
    NSLog(@"this is time in future in abs terms %f",timeInFuture);

    CFDateRef futureDate = CFDateCreate(NULL, timeInFuture);
    CFDateRef dateNow = CFDateCreate(NULL, [Timer getCurTime]);

    Boolean keepTesting = true;

    while (keepTesting) {
        if (CFDateCompare(dateNow, futureDate,NULL) == 0)   // ie both times are equal
        {
            NSLog(@"now is the time!");
            keepTesting = false;
        } else {
            NSLog(@"now isn't the time.. skip");
        }
    }
}

在Timer.m中:

-(void)setCurrentTimeAsReferencepoint
{
    _referencePoint = [[self class] getCurTime];
    NSLog(@"this is reference point %f",_referencePoint);  
}

+(double)getCurTime
{
    return (double)CFAbsoluteTimeGetCurrent();
}

// not used in the above code.. but used when i compare the time of the server phone with the client phone
+(double)getTimeDifference:(double)time1
                     time2:(double)time2
{    
    CFDateRef newDate = CFDateCreate(NULL, time2);
    CFDateRef oldDate = CFDateCreate(NULL, time1);

    CFTimeInterval difference = CFDateGetTimeIntervalSinceDate(newDate, oldDate);    

    NSLog(@"this is time difference %f",fabs(difference));
    CFRelease(oldDate); CFRelease(newDate); 

    // fabs = absolute value
    return fabs(difference);    
}

推荐答案

事实证明它比我想象的要简单(注意:getCurTime在问题中定义):

It turns out it's simpler than I thought (note: getCurTime is defined in the question):

-(void)performAction
{
    double curTime = [Timer getCurTime];
    double timeInFuture = 2 + curTime;
    NSLog(@"this is time in future in abs terms %f and this is curTime %f",timeInFuture, curTime);

    CFDateRef futureDate = CFDateCreate(NULL, timeInFuture);
    CFDateRef dateNow = CFDateCreate(NULL, curTime);

    Boolean keepTesting = true;

    while (keepTesting) {
        dateNow = CFDateCreate(NULL, [Timer getCurTime]);
        if (CFDateCompare(dateNow, futureDate,NULL) >= 0)   // ie both times are equal or we've 
                                                            // gone past the future date time
        { 
            NSLog(@"now is the time!");
            keepTesting = false;
            return;
        } else {
            NSLog(@"now isn't the time.. skip");
        }
    }
}

这篇关于如何设置一个以绝对术语表示未来时间的变量Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆