在递归函数运行期间更新UITextView [英] Update UITextView during recursive function is running
问题描述
我尝试更新主线程上的UITextView,但似乎没有设置
Maze.h是定义协议的对象,MainViewController.m是委托。
保存控制器的代码:
'MainViewController.m' code>
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:动画];
迷宫* aMaze = [[Maze alloc] initWithMaze:@Maze.txtandUpdate:true everyXSeconds:1];
[aMaze setDelegate:self];
if(![aMaze parseFile]){
exit(2);
}
if([aMaze solve:-1 y:-1 z:-1]){
NSLog(@%@,[aMaze printMazeHorizontally]);
NSLog(@Escapable:%@,[aMaze getMoveSequence]);
} else {
NSLog(@Unescapable);
exit(1);
}
}
- (void)didMakeMove :( NSString *)迷宫{
NSLog(@%@,迷宫);
dispatch_async(dispatch_get_main_queue(),^ {
[self.maze setText:maze];
});
}
'Maze.m'
- (BOOL)solve:(NSInteger)xy:(NSInteger)yz:(NSInteger)z
{
...
...
[self.delegate didMakeMove:self.printMazeVertically];
...
...
}
UITextView直到 - (BOOL)solve :::
完成运行,似乎没有更新。哪些只更新一次而不是多次。
不知道为什么会发生这种情况。
更新UITextView?
我认为更新UI应该在主线程上完成?
解决方案:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {
if([aMaze solve:-1 y:-1 z:-1] ){
NSLog(@%@,[aMaze printMazeHorizontally]);
NSLog(@Escapable:%@,[aMaze getMoveSequence]);
} else {
NSLog(@Unescapable);
exit(1);
}
});
绘图将在稍后的runloop运行或on下一个runloop运行。因此,如果您的递归运行时阻止主线程,则UI将不会更新,直到您结束递归。
考虑更改您的设计。将税收递归移动到后台线程,并使用GCD更新UI。
I have a view controller with a UITextView loaded into the frame. I want to update the text of the UITextView every time the function calls itself.
I attempt to update the UITextView on the main thread but it doesn't seem to set the text of the View UNTIL after the recursive function is done running.
'Maze.h' is the object that defines the protocol and 'MainViewController.m' is the delegate.
Heres the code for the controller:
'MainViewController.m'
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
Maze *aMaze = [[Maze alloc] initWithMaze:@"Maze.txt" andUpdate:true everyXSeconds:1];
[aMaze setDelegate:self];
if (![aMaze parseFile]) {
exit(2);
}
if ([aMaze solve:-1 y:-1 z:-1]){
NSLog(@"%@", [aMaze printMazeHorizontally]);
NSLog(@"Escapable: %@", [aMaze getMoveSequence]);
} else {
NSLog(@"Unescapable");
exit(1);
}
}
- (void)didMakeMove:(NSString *)maze {
NSLog(@"%@", maze);
dispatch_async(dispatch_get_main_queue(), ^{
[self.maze setText:maze];
});
}
'Maze.m'
- (BOOL)solve:(NSInteger)x y:(NSInteger)y z:(NSInteger)z
{
...
...
[self.delegate didMakeMove:self.printMazeVertically];
...
...
}
The UITextView just doesn't seem to update until -(BOOL)solve:::
is done running. Which only updates once instead of multiple times.
Not sure why this is happening.
Any ideas on how to update the UITextView?
I thought that updating the UI should be done on the main thread?
Solution:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([aMaze solve:-1 y:-1 z:-1]){
NSLog(@"%@", [aMaze printMazeHorizontally]);
NSLog(@"Escapable: %@", [aMaze getMoveSequence]);
} else {
NSLog(@"Unescapable");
exit(1);
}
});
Drawing is performed later in the runloop run, or on the next runloop run. Thus, if you block the main thread while your recursion is running, UI will not update until after you end your recursion.
Consider changing your design. Move the taxing recursion to a background thread, and update the UI using GCD.
这篇关于在递归函数运行期间更新UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!