此代码是否产生 2 个 NSTimer 而不是 1 个?

Does this code produce 2 NSTimers instead of 1?

我正在创建一个工作队列来在后台执行任务。代码如下。问题是定时器调用的选择器在每个周期被 2 个不同的定时器调用两次。

队列 (UpdateController) 在 AppDelegate 的 didFinishLaunchingWithOptions 中创建:

...
[self setUpdateController:[[FFUpdateController alloc] initWithRootDetailViewController:rdvc]];
[[self updateController] start];
...

这是 UpdateController 初始化程序

- (id) initWithRootDetailViewController:(FFRootDetailViewController*)rdvc
{
    if (self = [super init])
    {
        _rootDetailViewController = rdvc;
        _updateQueue = [[NSOperationQueue alloc] init];
    }

    return self;
}

这里是 UpdateController 开始

- (void) start
{
    //sweep once a minute for updates
    [self setTimer:[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(sweepForUpdates:) userInfo:nil repeats:YES]]; 
}

这里是sweepForUpdates,定时器调用的选择器:

- (void) sweepForUpdates:(NSTimer*)timer
{
    FormHeader* fh;
    NSInvocationOperation* op;
    NSInteger sectionIdx = [[self dataController] sectionIndexForFormTypeWithTitle:SFTShares];
    NSInteger headerCount = [[self dataController] numberOfRowsInSection:sectionIdx];
    NSArray* changed;
    NSMutableDictionary* params;

    NSLog(@"Notice - sweepForUpdates(1) called:");
    for (NSInteger i = 0; i < headerCount; i++)
    {
        fh = [[self dataController] formHeaderAtIndexPath:[NSIndexPath indexPathForRow:i inSection:sectionIdx]];
        changed = [[self dataController] formDatasModifiedSince:[fh modifiedAt] ForFormHeader:fh];

        if ([changed count])
        {
            NSLog(@"Error - sweepForUpdates(2) update: changes to update found");
            params = [[NSMutableDictionary alloc] init];
            [params setObject:fh forKey:@"formHeader"];
            [params setObject:[self rootDetailViewController] forKey:@"rootDetailViewController"];

            op = [[NSInvocationOperation alloc] initWithTarget:[FFParseController sharedInstance] selector:@selector(updateRemoteForm:) object:params];

            if ([[[self updateQueue] operations] count])
            {
                [op addDependency:[[[self updateQueue] operations] lastObject]];
            }
            [[self updateQueue] addOperation:op];
        }
        else
        {
            NSLog(@"Error - sweepForUpdates(3) save: no changes found");
        }
    }

    NSLog(@"Notice - sweepForUpdates(4) done:");
}

在这种情况下,有 2 个对象要检查更新。这是 1 次扫描的控制台输出:

2015-02-16 09:22:28.569 formogen[683:806645] Notice - sweepForUpdates(1) called:
2015-02-16 09:22:28.580 formogen[683:806645] Error - sweepForUpdates(3) save: no changes found
2015-02-16 09:22:28.583 formogen[683:806645] Error - sweepForUpdates(3) save: no changes found
2015-02-16 09:22:28.584 formogen[683:806645] Notice - sweepForUpdates(4) done:
2015-02-16 09:22:29.249 formogen[683:806645] Notice - sweepForUpdates(1) called:
2015-02-16 09:22:29.254 formogen[683:806645] Error - sweepForUpdates(3) save: no changes found
2015-02-16 09:22:29.256 formogen[683:806645] Error - sweepForUpdates(3) save: no changes found
2015-02-16 09:22:29.256 formogen[683:806645] Notice - sweepForUpdates(4) done:

两个对象都没有更新,这是正确的。但是我不明白为什么选择器被调用了两次。

谢谢

将日志记录添加到 start。你可能不止一次调用它。

请注意,UpdateController 永远无法解除分配,因为计时器正在保留它。这可能没问题,但如果您认为您正在释放它(及其计时器),请记住这一点。