Sleep or Pause NSThread

I create a new thread that launches one of my methods at every specific time interval.

Now what I am doing is as follows:

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil]; [thread start]; 

the above method calls my setUpTimerThraed method, and this method does NSTimer and calls a specific ie syncData method every 1 minute. Now what I want is my synchronization. The data method sends some files to the FTP folder. and when ever he sends one file to FTP, I want his "stream" to sleep for a while.

I used

 [NSThread sleepForTimeInterval:3.0]; 

But this code blocks my main application thread. Is there a way that I can only sleep the thread thread for a while, so that it will not stop my application for 3 seconds and will not sleep in my main thread. I hope that I have what I ask the guys about. Help PLease?

+4
source share
2 answers

sleepForTimeInterval: sleeps the thread on which it is being called. Try to cause a dream in the thread that you want to sleep, and not in the main thread.

replace current sleep call

 [self performSelector:@selector(sleepThread) onThread:thread withObject:nil waitUntilDone:YES]; 

then we implement this method

 -(void)sleepThread { [NSThread sleepForTimeInterval:3.0]; } 
+6
source

ColdLogic's solution is correct, but you must set the waitUntilDone flag to YES , otherwise the method will never be called.

0
source

All Articles