0

I want to stop calling loop function when View disapear. How can i do that? This is my code:

    -(void) viewWillAppear:(BOOL)animated
{
    [self performSelectorInBackground:@selector(updateArray)  withObject:nil];

}

And :

    -(void)updateArray
{

while (1)
{
    NSLog(@"IN LOOP");
   [NSThread sleepForTimeInterval:2.0];
....}

updateArray is callled usually when this View disappear. I want to stop call updateArray function

Thanks in advance

4 Answers4

2

Make an BOOL iVar or property

BOOL loopShouldRun;

in viewWillAppear set it to YES.

then use this code

-(void)updateArray
{
  while (loopShouldRun)
    {
      NSLog(@"IN LOOP");
      [NSThread sleepForTimeInterval:2.0];
....}
}

and in viewWillDisappear set it to NO.

But as @Michael Deuterman mentioned in the comments there could be a problem when the view disappears before the sleepTimer fires.

So heres is another solution with a NSTimer.

  • create a NSTimer iVar/@property: @property (strong) NSTimer *timer;
  • in viewWillAppear create the timer:

    timer = [NSTimer timerWithTimeInterval:2.0 invocation:@selector(updateArray) repeats:Yes]

  • in viewWillDiappear invalidate the timer:

    if ([self.timer isValid]) { [self.timer invalidate] }

your updateArray should look now like this:

-(void)updateArray {
  NSLog(@"in loop");
}
Pfitz
  • 7,336
  • 4
  • 38
  • 51
  • This isn't the optimal answer. The problem with this @Pfitz, is that the view may be disappeared and released before the long sleep time in "`sleepForTimeInterval: 2.0`" returns. – Michael Dautermann Jul 03 '13 at 07:15
  • timer = [NSTimer timerWithTimeInterval:2.0 invocation:@selector(updateArray) repeats:Yes] it error. It don't work – love_forever htkt Jul 03 '13 at 07:41
0
while (1)
{
    NSLog(@"IN LOOP");
   [NSThread sleepForTimeInterval:2.0];
}

while(1) will be true forever. To stop it, you need to have a condition that will stop the loop from happening.

For example,

while (1)
{
    NSLog(@"IN LOOP");
   [NSThread sleepForTimeInterval:2.0];
   if(something happens)
    break;
}

Hope it helps you.

lakshmen
  • 28,346
  • 66
  • 178
  • 276
0

this is a simple logic...just take one flag variable and update the value of that flag varibale when your viewdisapper then

 - (void)viewWillDisappear:(BOOL)animated 

above method method will call just before view disappear

viewDidDisappear:(BOOL)animated

above method is also there. that will be called just after your view did disappeared

so you can change your flag variable's value in one of above method.then according your flag variable value just put break in your while loop and your loop will broke .

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
0

Use NSThread instead of performSelector

NSThread *myThread; // preferable to declare in class category in .m file or in .h file

In viewWillAppear

myThread = [[NSThread alloc] initWithTarget:self selector:@selector(updateArray) object:nil];
[myThread start];

In viewWillDisappear

[myThread cancel]; // This will stop the thread and method will get stopped from execution
myThread = nil; // Release and nil object as we are re-initializing in viewWillAppear

For more details refer this : NSThread Class Reference

Mrunal
  • 13,982
  • 6
  • 52
  • 96