Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 退出计时器上的void方法_Ios_Objective C_Cocoa Touch_Nstimer - Fatal编程技术网

Ios 退出计时器上的void方法

Ios 退出计时器上的void方法,ios,objective-c,cocoa-touch,nstimer,Ios,Objective C,Cocoa Touch,Nstimer,我有一种与录制视频同时运行的方法。当该方法结束时,它会触发一系列其他方法,这些方法会一直持续到录制结束。我希望能够按下一个按钮来提前停止录制,同时也退出该方法。我目前尝试的方法是使用一个NSTimer来检查录制是否仍在进行,如果没有,它将停止播放音频,并且还应该调用return来停止该方法 -(void) method { self.stopTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@

我有一种与录制视频同时运行的方法。当该方法结束时,它会触发一系列其他方法,这些方法会一直持续到录制结束。我希望能够按下一个按钮来提前停止录制,同时也退出该方法。我目前尝试的方法是使用一个NSTimer来检查录制是否仍在进行,如果没有,它将停止播放音频,并且还应该调用return来停止该方法

-(void) method
{
    self.stopTimer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(checkRecording) userInfo:nil repeats:YES];

    // Stuff happens
}

-(void) checkRecording
{
if (isRecording == NO)
    {
        if (player.playing == YES)
        {
            [player stop];
        }
        return;
    }

}

这会立即停止音频,但该方法会继续运行,直到完成为止。它不会调用序列中的下一个方法,这是朝着正确方向迈出的一步,但我需要它立即停止。我唯一的理论是,这是因为我没有在实际方法中调用return,而是在另一个方法中,但即使是这样,我也不确定如何解决这个问题,因为据我所知,计时器只能指向其他方法,我不能告诉它我希望它在我想要停止的方法内部做什么。如果这不是问题所在,那么我真的不确定为什么这不起作用。

如果计时器有效,可以使其失效,从而停止计时器

我不确定所有检查是否真的有必要&最后一行,但我目前是这样做的:

if ( myTimer != nil && [myTimer isValid] )
{
    [myTimer invalidate];
    myTimer = nil;
}
编辑:

if ( [myTimer isValid] )
{
    [myTimer invalidate];
    myTimer = nil;
}
我唯一的理论是,这是因为我没有在实际方法中调用return,我想停止它,而是在另一个方法中

你的理论是正确的。return结束它所在的函数或方法,而不是其他函数或方法。它将当前函数的上下文从堆栈中弹出,并将执行返回给调用函数

我真的不知道如何修复它,因为据我所知,计时器只能指向其他方法,我不能告诉它我希望它在我想要停止的方法内部做什么

我们可以使用对象来存储状态,并使用该状态来控制程序的流程。该状态可以不断更新和检查。对于需要取消以响应该状态更改的长时间运行的任务,该状态必须与任务并行更新。既然您说计时器可以停止音频,但在方法中完成的工作却不能,那么我假设该方法已经在异步执行其长时间运行的任务

这需要在后台执行一个异步长时间运行的任务或一系列任务,并可能取消,这与NSOperation和NSOperationQueue类非常匹配

您可以通过实现方法或块在NSOperation对象内执行工作。实现您的代码以检查操作是否已在所有适当的时间取消,并尽快退出

下面是一个与您的用例相匹配的示例。它是在iOS应用程序“空应用程序”模板中创建的,所有内容都在应用程序代理中。我们的应用程序委托跟踪决定是否取消所需的状态,并安排计时器轮询该状态的更改。如果确定应该取消,则将实际取消的工作委托给操作队列及其操作

#import "AppDelegate.h"


@interface AppDelegate ()

@property (nonatomic) BOOL shouldStop; // Analogous to your isRecording variable
@property (nonatomic, strong) NSOperationQueue *operationQueue; // This manages execution of the work we encapsulate into NSOperation objects

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Typical app delegate stuff
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    // Start our long running method - analogous to method in your example
    [self method];

    return YES;
}


- (void)method
{
    // allocate operation queue and set its concurrent operation count to 1. this gives us basic ordering of
    // NSOperations. More complex ordering can be done by specifying dependencies on operations.
    self.operationQueue = [[NSOperationQueue alloc] init];
    self.operationQueue.maxConcurrentOperationCount = 1;

    //  We create three NSBlockOperations. They only sleep the thread a little while,
    //  check if they've been cancelled and should stop, and keep doing that for a few seconds.
    //  When they are completed (either through finishing normally or through being cancelled, they
    //  log a message
    NSMutableArray *operations = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {

        //  Block operations allow you to specify their work by providing a block.
        //  You can override NSOperation to provide your own custom implementation
        //  of main, or start, depending. Read the documentation for more details.
        //  The principle will be the same - check whether one should cancel at each
        //  appropriate moment and bail out if so

        NSBlockOperation *operation = [[NSBlockOperation alloc] init];

        //  For the "weak/strong dance" to avoid retain cycles
        __weak NSBlockOperation *weakOperation = operation;

        [operation addExecutionBlock:^{
            //  Weak/strong dance
            NSBlockOperation *strongOperation = weakOperation;

            //  Here is where you'd be doing actual work
            //  Either in a block or in the main / start
            //  method of your own NSOperation subclass.
            //  Instead we sleep for some time, check if
            //  cancelled, bail out if so, and then sleep some more.
            for (int i = 0; i < 300; i++) {
                if ([strongOperation isCancelled]) {
                    return;
                }
                usleep(10000);
            }
        }];

        //  The completion block is called whether the operation is cancelled or not.
        operation.completionBlock = ^{
            //  weak/strong dance again
            NSBlockOperation *strongOperation = weakOperation;
            NSLog(@"Operation completed, %@ cancelled.", [strongOperation isCancelled] ? @"WAS" : @"WAS NOT");
        };

        [operations addObject:operation];
    }

    //  Set up a timer that checks the status of whether we should stop.
    //  This timer will cancel the operations if it determines it should.
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkShouldKeepGoing:) userInfo:nil repeats:YES];

    //  Use GCD to simulate a stopped recording to observe how the operations react to that.
    //  Comment out to see the usual case.
    double delayInSeconds = 5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        self.shouldStop = YES;
    });

    //  Add the operations to the operation queue, exeuction will start asynchronously from here.
    [self.operationQueue addOperations:operations waitUntilFinished:NO];
}


//  If we should stop, cancel the operations in the queue.
- (void)checkShouldKeepGoing:(NSTimer *)timer
{
    if (self.shouldStop) {
        NSLog(@"SHOULD STOP");
        [timer invalidate];
        [self.operationQueue cancelAllOperations];
    }
}

@end

如果你想让计时器停止启动,你需要使它失效。[self.stopTimer invalidate]正在检查!=零的确是不必要的;[nil isValid]评估为否。感谢您的澄清!伙计,这比我想象的要复杂得多,但是谢谢!我会仔细看看你发布的内容,看看我是否能想出如何让它满足我的需要;我真的很感谢你的帮助@我希望我没有把我的例子复杂化。这其实并不十分困难,但我需要组合一些计时器和模块来尝试建模您的问题,希望它们不会太严重地模糊要点。我忘了推荐一件事,那就是看看苹果的。如果你有任何问题或者我能澄清我的答案,请告诉我。