这段代码是否产生2个NSTimers而不是1个?

时间:2015-02-16 15:37:05

标签: xcode nstimer nsoperationqueue

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

队列(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:

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

由于

1 个答案:

答案 0 :(得分:1)

将记录添加到start。你可能不止一次叫它。

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