iPhone dev - performSelector:withObject:afterDelay或NSTimer?

时间:2009-09-29 00:41:38

标签: iphone nstimer tail-recursion repeat

要重复方法调用(或消息发送,我猜适当的术语是)每 x 秒,是否更好地使用NSTimer(NSTimer的scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:或者让方法在末尾以递归方式调用自身(使用performSelector:withObject:afterDelay)?后者不使用对象,但可能不太清晰/可读?另外,为了让您了解我正在做什么,它只是一个带有标签的视图,倒计时到午夜12点,当它变为0时,它会闪烁时间(00:00:00)并永远发出哔哔声。

感谢。

编辑:同样,重复播放SystemSoundID(永远)的最佳方法是什么? 编辑:我最终使用它来永远播放SystemSoundID:

// Utilities.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioServices.h>


static void soundCompleted(SystemSoundID soundID, void *myself);

@interface Utilities : NSObject {

}

+ (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type;
+ (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID;
+ (void)stopPlayingAndDisposeSystemSoundID;

@end


// Utilities.m
#import "Utilities.h"


static BOOL play;

static void soundCompleted(SystemSoundID soundID, void *interval) {
    if(play) {
        [NSThread sleepForTimeInterval:(NSTimeInterval)interval];
        AudioServicesPlaySystemSound(soundID);
    } else {
        AudioServicesRemoveSystemSoundCompletion(soundID);
        AudioServicesDisposeSystemSoundID(soundID);
    }

}

@implementation Utilities

+ (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type {
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type];
    SystemSoundID soundID;

    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
    return soundID;
}

+ (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID interval:(NSTimeInterval)interval {
    play = YES
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,
                                          soundCompleted, (void *)interval);
    AudioServicesPlaySystemSound(soundID);
}

+ (void)stopPlayingAndDisposeSystemSoundID {
    play = NO
}

@end

似乎工作正常..对于标签闪烁,我会使用NSTimer。

3 个答案:

答案 0 :(得分:6)

计时器更适合严格定义的间隔。如果你的函数调用本身有一个延迟,你将失去准确性,因为它不是真正同步到一个时间间隔。总是需要花时间来运行实际的方法本身,这会将间隔排除在外。

坚持使用NSTimer,我会说。

答案 1 :(得分:2)

只是为其他答案添加一点,递归调用的情况是调用可能需要一段未知的时间 - 比如说你用少量数据重复调用Web服务,直到完成为止。每次调用可能需要一些未知的时间,因此在Web调用返回之前,代码不执行任何操作,然后发送下一批,直到不再发送数据并且代码不会再次调用自身。

答案 2 :(得分:1)

由于您的应用程序取决于时间准确性(即它需要每秒执行一次),因此NSTimer会更好。方法本身需要一些时间来执行,并且NSTimer可以正常使用(只要你的方法花费不到1秒,如果它每秒调用一次)。

要重复播放声音,您可以设置完成回叫并重播声音:

SystemSoundID tickingSound;

...

AudioServicesAddSystemSoundCompletion(tickingSound, NULL, NULL, completionCallback, (void*) self);

...

static void completionCallback(SystemSoundID mySSID, void* myself) {
  NSLog(@"completionCallback");

  // You can use this when/if you want to remove the completion callback
  //AudioServicesRemoveSystemSoundCompletion(mySSID);

  // myself is the object that called set the callback, because we set it up that way above
  // Cast it to whatever object that is (e.g. MyViewController, in this case)
  [(MyViewController *)myself playSound:mySSID];
}