用 NSTimer 替换 usleep? [英] Replace usleep with NSTimer?

查看:57
本文介绍了用 NSTimer 替换 usleep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在以下代码中将 usleep 替换为 NSTimer:

How can I substitute usleep with NSTimer in the following code:

/**
 * DETERMINATE BAR with LABEL
 */

- (void)showDeterminateBarWithLabel:(CDVInvokedUrlCommand *)command {

    // obtain commands
    bool dim = [[command.arguments objectAtIndex:0] boolValue];
    int increment = [[command.arguments objectAtIndex:1] intValue];
    NSNumber* incrementValue = @(increment);
    NSString* text = [command.arguments objectAtIndex:2];

    // initialize indicator with options, text, detail
    self.progressIndicator = nil;
    self.progressIndicator = [MBProgressHUD showHUDAddedTo:self.webView.superview animated:YES];
    self.progressIndicator.mode = MBProgressHUDModeDeterminateHorizontalBar;
    self.progressIndicator.labelText = text;


    // Check for dim : true ? false
    if (dim == true) {
        self.progressIndicator.dimBackground = YES;
    }

    // Load Progress bar with ::incrementValue
    [self.progressIndicator showWhileExecuting:@selector(progressTask:) onTarget:self withObject:incrementValue animated:YES];

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)progressTask:(NSNumber *)increment{

    // get increment value
    int _increment = [increment intValue];

    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        self.progressIndicator.progress = progress;

        // increment in microseconds (100000mms = 1s)
        usleep(_increment);
    }
}

此代码取自此处.

推荐答案

你不能. 这两者完全不同,这段代码需要阻塞操作.因为它是在后台线程上执行的.

You cannot. Those two are quite different and this code requires blocking operation. Because it’s executed on a background thread.

方法 -progressTask:这个方法,在新线程:

- (void)launchExecution {
    @autoreleasepool {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        // Start executing the requested task
        [targetForExecution performSelector:methodForExecution withObject:objectForExecution];
#pragma clang diagnostic pop
        // Task completed, update view in main thread (note: view operations should
        // be done only in the main thread)
        [self performSelectorOnMainThread:@selector(cleanUp) withObject:nil waitUntilDone:NO];
    }
}

它依赖于同步执行,使用 NSTimer 将需要启动 NSRunLoop 并让它运行一段时间,这实际上是可能的,但 只是不要't.

It relies on synchronous execution and using NSTimer would requre starting NSRunLoop and letting it run for some time, which would be actually possible, but just don’t.

提示:如果您更喜欢 Objective-C 方法,请使用以秒为单位的参数调用 +[NSThread sleepForTimeInterval:].

Hint: If you prefer Objective-C approach, call +[NSThread sleepForTimeInterval:] with argument in seconds.

这篇关于用 NSTimer 替换 usleep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆