samedi 27 juin 2015

Stop NSRunloop in NSOperate

I Know is maybe a bad design,but all i want to do is run a nsTimer in NSOperation,so here is what i done:

            #import "CountOperate.h"

            @interface CountOperate ()

            @property(nonatomic, strong) NSTimer *timer;
            @property(nonatomic, unsafe_unretained) NSInteger repeatSecond;

            @end

            @implementation CountOperate

            - (void)main
            {
                self.repeatSecond = 15;

                self.timer = [NSTimer timerWithTimeInterval:self.repeatSecond target:self selector:@selector(update) userInfo:nil repeats:YES]; 
                [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 
                [[NSRunLoop currentRunLoop] run]; 
            }

            - (void)cancel
            {
                [self.timer invalidate];
                [super cancel];
            }

            - (void)update
            {
                NSUInteger myRandom = arc4random_uniform(100);
                self.dateArray[self.myID] = @(myRandom);     
                [self.tableView performSelectorOnMainThread:@selector(reloadData)
                                                 withObject:nil
                                              waitUntilDone:NO];
                [[NSRunLoop currentRunLoop] performSelector:@selector(update) withObject:nil afterDelay:self.repeatSecond];
            }

The problem here is that I can not stop it immediately, it must wait to be completed after the completion of the interval timer, it's like NSRunLoop must run until the next time the timer fires .

In this example,i had wait 15second,so i can see the NSOperation has finish its job.

Aucun commentaire:

Enregistrer un commentaire