Подтвердить что ты не робот

Выполнить метод после задержки, не используя NSThread или sleep() - есть ли другой вариант?

Есть ли способ в iOS4 вызывать метод после задержки с помощью чего-то другого, кроме NSThread или блокирования пользовательского интерфейса с помощью функции sleep()?

4b9b3361

Ответ 1

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // code to be executed on main thread.If you want to run in another thread, create other queue
    });

Ответ 2

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Of NSObject сделает это за вас.

Кроме того, вы можете создать NSTimer и выполнить обратный вызов для какой-либо цели и селектора через заданный промежуток времени.

Ответ 3

Вы можете использовать NSTimer. (например, timerWithTimeInterval:target:selector:userInfo:repeats)

Ответ 4

[self performSelector:@selector(methodName) withObject:nil afterDelay:2.0];

Ответ 5

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self 
         selector:@selector(aMethod:) userInfo:nil repeats:NO];

- (void) aMethod:(NSTimer *) aTimer {
  //task to do after the delay.
}

Ответ 6

Вы можете легко сделать это с помощью

[self performSelector:@selector(methodName) withObject:nil afterDelay:5];