4

I am creating a new Thread which runs one of my method after every certain period of time.

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 makes an NSTimer and calls a specific method i.e syncData after every 1 minute. Now what i want is my sync Data method is sending some files to the FTP folder. and when ever it send one file to the FTP I want it my "thread" to be sleep for a while.

i have used

[NSThread sleepForTimeInterval:3.0];

But this code blocks my application main thread. Is there any way i can only sleep my "thread" thread for a while so that it wont stop my application responding for 3 seconds and does not sleep my main thread. Hope u get what i am asking guys. PLease help?

johndpope
  • 5,035
  • 2
  • 41
  • 43
Shah
  • 4,990
  • 10
  • 48
  • 70

2 Answers2

6

sleepForTimeInterval: sleeps the thread that it is called on. Try calling a sleep on the thread you want to sleep rather than the main thread.

replace your current sleep call with

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

then implement this method

-(void)sleepThread {
    [NSThread sleepForTimeInterval:3.0];
}
ColdLogic
  • 7,206
  • 1
  • 28
  • 46
0

The solution of ColdLogic is right, but it's neccesary put the flag waitUntilDone to YES, otherwise the method is never called.

Ber.to
  • 1,064
  • 1
  • 10
  • 15