Thursday, August 25, 2011

performSelector: withObject: afterDelay: issue

I have discovered interesting issue today. I have the method for updating some info:
- (void) updateObject {
  // updating code

  // schedule next updating
  [self performSelector:@selector(updateObject) withObject:nil afterDelay:4.0];
  NSLog(@"done");
}
I figured out that updateObject is called twice! Second call was made immediately after first call was finished. First of all, i reread SDK about (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay method:
aSelector
  A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
It seems like my method is perfectly fits for this description. Ok. I searched internet for same issue and found this solution: method passed to performSelector MUST have an argument! I changed my method. It takes now one parameter:
- (void) updateObject:(id)something {
  // updating code

  // schedule next updating
  [self performSelector:@selector(updateObject:) withObject:nil afterDelay:4.0];
  NSLog(@"done");
}
Finally! It works perfect. It seems like iOS SDK contains inaccurate description for performSelector.

1 comment:

  1. Actually, you *can* in fact use a selector that takes no argument, but you must not include a colon after the name of the selector in order to indicate that it does not take any arguments, i.e. you must change to "@selector(updateObject)".

    ReplyDelete