Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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-目标C-退出视图时如何停止执行周期函数?_Ios_Objective C_Nstimer - Fatal编程技术网

IOS-目标C-退出视图时如何停止执行周期函数?

IOS-目标C-退出视图时如何停止执行周期函数?,ios,objective-c,nstimer,Ios,Objective C,Nstimer,我的一个ViewController中有一个函数,每10秒执行一次 我想让这个函数在我退出视图时停止执行 我尝试了这种代码的和平: -(void)viewWillDisappear:(BOOL)animated { NSError *error2; if ([_managedObjectContext save:&error2] == NO) { NSAssert(NO, @"Save should not fail\n%@", [error2 local

我的一个ViewController中有一个函数,每10秒执行一次

我想让这个函数在我退出视图时停止执行

我尝试了这种代码的和平:

-(void)viewWillDisappear:(BOOL)animated
{
    NSError *error2;
    if ([_managedObjectContext save:&error2] == NO) {
        NSAssert(NO, @"Save should not fail\n%@", [error2 localizedDescription]);
        abort();
    }
    else
        NSLog(@"Context Saved");



    [self stopTimer];
    NSLog(@"View will disappear now");
}
它基本上是调用stopTimer方法,该方法将为计时器提供null值

- (void) stopTimer
{
    [timer invalidate];
    timer = nil;
}
我的问题是,即使我离开我的视线,我的函数也会继续执行。永不停止。我怎样才能解决这个问题

编辑:

这是我的nstimer调用的函数:

- (void) MyFunctionCalledByNSTimer
{
     [timer invalidate];
     timer  =  [NSTimer scheduledTimerWithTimeInterval:10.0f
                                     target:self selector:@selector(Function1) userInfo:nil repeats:YES];

}
我在viewController的.m中声明我的nstimer

NSTimer *timer;
如果您需要更多的代码,请提问,我将编辑此问题。

使用此代码 在主线程上调用stopTimer

-(void)viewWillDisappear:(BOOL)animated
{
    NSError *error2;
    if ([_managedObjectContext save:&error2] == NO) {
        NSAssert(NO, @"Save should not fail\n%@", [error2 localizedDescription]);
        abort();
    }
    else
        NSLog(@"Context Saved");


    dispatch_async(dispatch_get_main_queue(), ^{
     //Your main thread code goes in here
     [self stopTimer];       
    });


    NSLog(@"View will disappear now");
}

请记住,必须从安装计时器的线程发送失效消息。如果您从另一个线程发送此消息,则与计时器关联的输入源可能不会从其运行循环中删除,这可能会阻止线程正确退出。

可能是因为创建了多个计时器,并且仅使您所引用的内容无效

因此,可能需要修改
MyFunctionCalledByStimer
,如下所示将解决您的问题:

- (void) MyFunctionCalledByNSTimer
{
     if(!timer){

          timer  =  [NSTimer scheduledTimerWithTimeInterval:10.0f
                                     target:self selector:@selector(Function1) userInfo:nil repeats:YES];
     }

}

现在,只有一个计时器引用将在那里,
[timer invalidate]
将使计时器无效。

我刚刚尝试了你的代码,它对我不起作用,因为即使我退出了它的视图,函数仍在执行。您认为在ViewWillEnglish中调用stoptimer方法正确吗?因为我在一些问题的答案中看到了与我类似的问题,人们在ViewDidDiscosearus中调用stoptimer,我们也可以从以前的viewcontroller viewwillappper方法中发出通知,还可以检查计时器是否有内存或内存not@suthar:viewwillEnglish总是在主线程上被调用:)为什么调度异步(调度获取主队列(),^{}有时它会导致问题,这就是为什么
1)
您的
视图将消失:
将无法编译,因为它缺少
}
-请添加完整、正确的方法<代码>2)您没有调用
[超级视图将消失:动画]-请将此添加到您的方法中<代码>3)
请添加由计时器调用的函数,该函数将持续运行到问题<代码>4)请添加声明计时器及其将运行的方法的代码。是否可以添加创建nstimer的代码或创建nstimer的位置的代码?@robotcat我们不需要调用超级视图,除非我们要覆盖它,否则它将消失。该方法将被调用,即使我们没有调用super@我不认为它缺少},因为我的“else”语句中只有一条指令,所以我不需要使用{..}。我将添加由我编辑问题的计时器调用的函数,请签出。@Ronakhaniyara我编辑了问题,请签出。您好,很抱歉,我刚刚意识到,即使在我实现此解决方案后,我的函数仍在执行。我现在很困惑,昨天它似乎在工作,而现在它不是。我希望你能帮助我,如果你想要更多关于我的代码的信息,我是可用的
[计时器失效]
之后,无法看到其他问题here@samouray