Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

synchronization - How to set a variable that represents a time in the future in absolute terms 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.

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");
        }
    }
}

in 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);    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...