구글에 다음으로 검색


nstimer multi thread




이건 메인스레드에서만 사용가능한건가?


백그라운드 개념도 있던데,  사용불가한가?


NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:NO];

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


    self.timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(countDown) userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoopaddTimer:self.timer forMode:NSRunLoopCommonModes];



NSRunLoopCommonModes : 


모드로 이것을 사용해야, 보통은 다른 작업을 할때, 타이머가 멈추는데..

타이머가 멈추는 것을 막을 수 있다





NSRunLoop를 사용


http://jaminya.com/index.php/en/component/content/article/39-ios/54-multithreading-nstimer


069.-(void) startOperation {
070. 
071.    // run task A for 1/10 second
072.    timerA = [NSTimer timerWithTimeInterval:1.0/10.0
073.                                              target:self
074.                                            selector:@selector(taskA)
075.                                            userInfo:nil
076.                                             repeats:YES];
077.     
078.    // run task B for 1/10 second
079.    timerB = [NSTimer timerWithTimeInterval:1.0/10.0
080.                                     target:self
081.                                   selector:@selector(taskB)
082.                                   userInfo:nil
083.                                    repeats:YES];
084. 
085.    NSRunLoop *loop = [NSRunLoop mainRunLoop];
086.    [loop addTimer:timerA forMode:NSDefaultRunLoopMode];
087.    [loop addTimer:timerB forMode:NSDefaultRunLoopMode];
088. 
089.}
090. 
091.-(void) stopOperation
092.{
093.    [timerA invalidate];
094.    [timerB invalidate];
095.}
096. 

- See more at: http://jaminya.com/index.php/en/component/content/article/39-ios/54-multithreading-nstimer#sthash.gG5iI1L0.dpuf






타이머 셋팅부분


If you need this so timers still run when you scroll your views (or maps), you need to schedule them on different run loop mode. Replace your current timer:

[NSTimer scheduledTimerWithTimeInterval:0.5
                                 target:self
                               selector:@selector(timerFired:)
                               userInfo:nil repeats:YES];

With this one:

NSTimer *timer = [NSTimer timerWithTimeInterval:0.5
                                           target:self
                                         selector:@selector(timerFired:)
                                         userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];



참고하면 좋을듯


http://stackoverflow.com/questions/8304702/how-do-i-create-a-nstimer-on-a-background-thread






구현된 상세코드

-------- 샘플 코드 ----------

void _funcTimer()

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    

    NSString * strTime =[ NSString stringWithFormat:@"%@", [formatter stringFromDate:[NSDate date]]];

    

    NSLog(@"%@", strTime);

    

    [formatter release];

}


+(dispatch_source_t) setTimer

{    

    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,

                                                     0, 0, dispatch_get_main_queue());

    

    uint64_t interval = 2ull*NSEC_PER_SEC; // NSEC_PER_MSEC

    

    uint64_t leeway = 1ull*NSEC_PER_SEC;

    

    if (timer)

    {

        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);

        

        dispatch_source_set_event_handler_f(timer, (dispatch_function_t)&_funcTimer);

        

        dispatch_resume(timer);

    }

    

    return timer;

}


+(void) endTimer:(dispatch_source_t) timer

{

    if (timer)

        dispatch_release(timer);

    

    timer = nil;

}



TCP/IP 사용 관련하여 코딩되어있는부분.

알맞은 부분만 추출해 볼 필요 있음


NSTimer *readtimer;
NSThread *readthread;


 readtimer= [[NSTimer scheduledTimerWithTimeInterval:TimerTime target:self 
            selector:@selector(recvthreadfunc:)
            userInfo:nil
             repeats:YES]retain];
 
 close(sockfd);
 
    [super viewDidLoad];
}

-(void*)recvthreadfunc:(NSTimer *)aTimer{
 readthread = [[NSThread alloc] initWithTarget:self selector:@selector(recvthreadfunc:)object:nil];
 [readthread start];
 return 0;
}
- (void *)recvthreadfunc:(id)anObject{
 if((n=read(c_socket , recvbuff,sizeof(recvbuff)))>0){
  recvbuff[n]='\0';
  NSLog(@"%s",recvbuff);
  write(c_socket,recvbuff,n);
 }


Posted by 스타켄지니어
,