How to set a variable that represents a time in the future in absolute terms Objective-C

前端 未结 1 728
北海茫月
北海茫月 2020-12-20 09:45

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 th

相关标签:
1条回答
  • 2020-12-20 10:07

    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");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题