I have an Array that have stored times. I want NStimer to trigger at times stored in that array. For example, in array a[] i have 2, 5 and 10 seconds and want NSTimer to trigger a method at these times. I do have another NSTimer that keep updating the time instance variable every second.
Asked
Active
Viewed 124 times
1 Answers
0
A simple solution would be to loop through your array and schedule method calls with GCD
for (NSNumber *time in array) {
NSTimeInterval delay = [time doubleValue];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
<#code to be executed after a specified delay#>
});
}

YarGnawh
- 4,574
- 6
- 26
- 37
-
1What would i do if my times are in milliseconds, and if i have to pause the array and also have to read from different points in array . Because for loop won't stop and keep executing till the end point – Asif Jun 25 '14 at 19:25