简体   繁体   中英

how to use dispatch_async

I implemented a thread with dispatch, but the code works fine, but the progress UI does not work

This is my code

@interface thirdController () {
    float progress;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    progress = 0.0;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self progressDeny];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self setProgress];
        });
    });
}

progressDeny

- (void)progressDeny {
    while (1) {
        if (progress >= 0 && progress <= 1.0) {
            NSLog(@"progress - 0.005!");
            progress -= 0.005;
            usleep(100000);
        }
    }
}

setProgress

- (void)setProgress {
    NSLog(@"%f", progress);
    [clickedProgress setProgress:progress animated:YES];
}

I saw this

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    //Background Thread
    dispatch_async(dispatch_get_main_queue(), ^(void){
        //Run UI Updates
    });
});

Why is the ui update part not working?

First, the sleep time in your progressDeny method is a bit long, so you can make it smaller. Second, the while (1){} in your progressDeny method is a endless loop and the method never returns, you can try to change it like this, for example:

- (void)progressDeny {
        if (progress >= 0 && progress <= 1.0) {
            NSLog(@"progress - 0.005!");
            progress -= 0.005;
            usleep(10);
        }
}

The code is not working fine if the purpose of it is present progress view & update its value. You made at least 2 bugs to make progress view work.

Let take a look into your code:

First, you init progress with 0.0

progress = 0.0;

Then, inside progressDeny , you subtracted it if it is equal to 0 & did not provide any way to exit the loop. This will ended up being run once then stuck in doing-nothing infinite loop.

- (void)progressDeny {
    while(1) {
        if (progress >= 0 && progress <= 1.0) {
            // Did you mean: progress += 0.005 ?
            progress -= 0.005;
            // ...
        }
    }
}

Now, let refactor your code to make it work:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    while (progress <= 1.0) {
        progress += 0.005;
        dispatch_async(dispatch_get_main_queue(), ^{
            [clickedProgress setProgress:progress];
        });
        usleep(100000);
    }
});

Or you can make it with NSTimer instead of GCD :

[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
    if (progress <= 1.0) {
        progress += 0.005;
        [clickedProgress setProgress:progress];
    } else {
        [timer invalidate];
        timer = nil;
    }
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM