Are there concurrency problems when using -performSelector:withObject:afterDelay: ?

Posted by mystify on Stack Overflow See other posts from Stack Overflow or by mystify
Published on 2010-05-16T10:06:12Z Indexed on 2010/05/16 10:10 UTC
Read the original article Hit count: 158

Filed under:
|

For example, I often use this:

[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:someDelay];

Now, lets say I call this 10 times to perform at the exact same delay, like:

[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];


- (void)doSomethingAfterDelay:(id)someObject {
   /*
   access an array, read stuff, write stuff, do different things that would suffer in multithreaded environments .... all operations are nonatomic!
   */
}

I have observed pretty strange behavior when doing things like this. For my understanding, this method schedules a timer to fire on the current thread, so in this case the main thread. But since it doesn't create new threads, it actually should not be possible to run into concurrency problems, right?

© Stack Overflow or respective owner

Are there concurrency problems when using -performSelector:withObject:afterDelay: ?

Posted by mystify on Stack Overflow See other posts from Stack Overflow or by mystify
Published on 2010-05-16T10:05:13Z Indexed on 2010/05/16 10:10 UTC
Read the original article Hit count: 158

Filed under:
|

For example, I often use this:

[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:someDelay];

Now, lets say I call this 10 times to perform at the exact same delay, like:

[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];
[self performSelector:@selector(doSomethingAfterDelay:) withObject:someObject afterDelay:2.0];


- (void)doSomethingAfterDelay:(id)someObject {
   /*
   access an array, read stuff, write stuff, do different things that would suffer in multithreaded environments .... all operations are nonatomic!
   */
}

I have observed pretty strange behavior when doing things like this. For my understanding, this method schedules a timer to fire on the current thread, so in this case the main thread. But since it doesn't create new threads, it actually should not be possible to run into concurrency problems, right?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about multithreading