I am creating a work queue to perform tasks in the background. The code is below. The problem is that selector called by the timer is called twice every period, by 2 different timers.
The queue (UpdateController) is created in didFinishLaunchingWithOptions of the AppDelegate:
...
[self setUpdateController:[[FFUpdateController alloc] initWithRootDetailViewController:rdvc]];
[[self updateController] start];
...
Here’s the UpdateController initializer
- (id) initWithRootDetailViewController:(FFRootDetailViewController*)rdvc
{
if (self = [super init])
{
_rootDetailViewController = rdvc;
_updateQueue = [[NSOperationQueue alloc] init];
}
return self;
}
Here’s UpdateController start
- (void) start
{
//sweep once a minute for updates
[self setTimer:[NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(sweepForUpdates:) userInfo:nil repeats:YES]];
}
Here is sweepForUpdates, the selector called by the timer:
- (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:");
}
In this case there are 2 objects to examine for updates. Here is the console output for 1 sweep:
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:
Neither object has updates, which is correct. But I do not understand why the selector is called twice.
Thanks
Add logging to start
. You probably call it more than once.
Note that UpdateController
can never deallocate, because the timer is retaining it. That may be ok, but keep that in mind if you believe you're deallocating it (and its timer).