with tutorial the español applications objective-c sync code-timing

objective-c - tutorial - the django project



Cómo establecer una variable que represente un tiempo en el futuro en términos absolutos objetivo C (1)

Resulta que es más simple de lo que pensaba (nota: getCurTime se define en la pregunta):

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

Motivación: estoy trabajando en una aplicación que hace que varios teléfonos (clientes) lean datos de audio de un teléfono (servidor). La idea es que todos deben tocar la canción al mismo tiempo juntos.

Premisa: debo encontrar la manera de hacer que todos los teléfonos comiencen en una marca de tiempo determinada que sea absoluta (es decir, no es relativa al reloj de uso establecido de ninguno de los teléfonos, etc.). Basándome en algunas investigaciones , pensé que la mejor manera hacer esto es usar CFAbsoluteTimeGetCurrent(); La idea aquí es que tengo la latencia que toma el servidor para comunicarse con cada teléfono (b / c GKSession se realiza en serie no en paralelo aparentemente), agregue esa latencia a la hora actual, y luego haga que cada teléfono reproduzca la canción comenzando en esa hora de inicio Esta hora de inicio debe ser absoluta, no puede ser relativa.

Pregunta: ¿Cómo puedo representar un tiempo en el futuro en un número, que luego puede usarse para construir un CFDateRef ? (La razón por la que debe expresarse como un número es que debo poder enviarlo en un paquete ...)

Ejemplo: Esto es un poco de código que explica lo que estoy tratando de lograr:

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

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