Reputation: 13297
In my app, I use operations to perform time-intensive tasks, so my user interface won't freeze. For that, I use NSInvocationOperation. I wanted to test the overall architecture first before implementing the code to actually complete the tasks, so that's what I have right now:
// give the object data to process
- (void)processData:(NSObject*)dataToDoTask {
... // I store the data in this object
NSInvocationOperation *newOperation =
[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(performTask)
object:nil];
[[NSOperationQueue mainQueue] addOperation:newOperation];
...
}
// process data stored in the object and return result
- (NSObject*)performTask {
[NSThread sleepForTimeInterval:1]; // to emulate the delay
return [NSString stringWithFormat:@"unimplemented hash for file %@", self.path];
}
However, the sleep doesn't work as I expect: instead of delaying the operation completetion, it freezes the app. It seems that I either operations or sleep incorrectly, but I can't figure out which and how.
Upvotes: 1
Views: 835
Reputation: 11780
That is because you are running your operation on the main thread (the same running your user interface).
If you want to run your operation concurrently, create a new operation queue:
NSOperationQueue * queue = [NSOperationQueue new];
[queue addOperation:newOperation];
Upvotes: 3