Shah
Shah

Reputation: 5020

Sleep or Pause NSThread

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?

Upvotes: 4

Views: 6212

Answers (2)

ColdLogic
ColdLogic

Reputation: 7265

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];
}

Upvotes: 6

Ber.to
Ber.to

Reputation: 1104

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

Upvotes: 0

Related Questions