I am having problems updating my customView object during simulation. The window pops up after the simulation is done. I would like it to update itself during the simulation. For this I use setNeedsDisplay:YES
and I have also tried display
. None of this works for me however. Does anyone have an idea how I should get this working? As you can see below I have tried to create a new thread for the updating as well as using NSOperations. Grateful for help!
//Run simulation
for (int iteration=0; iteration<numberOfIterations; iteration++){
//NSInvocationOperation *update = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updatePopulation) object:nil];
//NSInvocationOperation *draw = [[NSInvocationOperation alloc] initWithTarget:view selector:@selector(redraw) object:nil];
//[draw addDependency:update];
//[queue addOperation:update];
//[queue addOperation:draw];
[NSThread sleepForTimeInterval:0.01]; //to make it easer to see..
[self updatePopulation];
//[view redraw];
[NSThread detachNewThreadSelector:@selector(redraw) toTarget:view withObject:nil];
//[self performSelector:@selector(updatePopulation) withObject:nil afterDelay:1];
//[view performSelector:@selector(redraw) withObject:nil afterDelay:1];
//Save segregation
if (iteration%(numberOfIterations/100) == 0) {
printf("hej\n");
}
}
in my viewer class:
- (void) redraw {
//[self setNeedsDisplay:YES];
[self display];开发者_如何学JAVA
}
It looks like you are trying to do the painting on the worker thread.
This is not, as far as I am aware, supported.
To solve this, you need to move your simulation to a worker thread, and then use performSelectorOnMainThread: to invoke the redraw on the main thread. I find this article on threading in cocoa to be required reading when trying to implemented threaded cocoa apps.
精彩评论