Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2011, 05:44 PM   #1 (permalink)
Registered Member
 
DeeJay_CRO's Avatar
 
Join Date: Feb 2011
Location: Osijek, Croatia
Posts: 64
DeeJay_CRO is on a distinguished road
Default Simple (hopefully) thread question

Hello!

I never needed to work with threads till now - my task shouldn't be complicated.

So, I have one label and I need to change label text every 10 seconds - in endless loop.

Something like this:

Code:
-(void) changeText {
    [myLabel setText:@"Text1"];

    <wait 10 seconds>

   [myLabel setText:@"Text2"];

    <wait 10 seconds>
}
And this Text1 and Text2 has to exchange each other constantly.
And application itself must not be frozen due to this text change.
(Therefore I suppose that this changing text should be in a thread).

Please, can you give me some instructions how to do it.

Thanks a lot!
DeeJay_CRO is offline   Reply With Quote
Old 05-26-2011, 05:49 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

NSTimer
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 05-26-2011, 07:51 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

As Brian said, "NSTimer" is what you should use. However you should also realize that this cannot be done with threads. The only thread that is allowed to touch the user interface (meaning the display screen) is the main user interface thread. Worker threads can do work, but they must not try to talk directly to the user.

If you look at how NSTimer can be made to send a message to the UI run loop every so often, you will see that threads are not even needed to do what you need to do.
RLScott is offline   Reply With Quote
Old 05-27-2011, 06:28 AM   #4 (permalink)
Registered Member
 
DeeJay_CRO's Avatar
 
Join Date: Feb 2011
Location: Osijek, Croatia
Posts: 64
DeeJay_CRO is on a distinguished road
Default

Quote:
Originally Posted by RLScott View Post
As Brian said, "NSTimer" is what you should use. However you should also realize that this cannot be done with threads. The only thread that is allowed to touch the user interface (meaning the display screen) is the main user interface thread. Worker threads can do work, but they must not try to talk directly to the user.

If you look at how NSTimer can be made to send a message to the UI run loop every so often, you will see that threads are not even needed to do what you need to do.
Hello!

Thanks for the answers. I did it in the following way:
Code:

- (void) thisMethodIsTrigered {
NSLog(@"setMessage should be triggered now");
    NSTimer *messageTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                    target: self
                                    selector:@selector(setMessageText:)
                                    userInfo:nil
                                    repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:messageTimer forMode:NSDefaultRunLoopMode];

    
}

- (void)setMessageText {
    NSLog(@"setMessage triggered !!!");
}
In the console, I see only:
2011-05-27 13:15:48.924 Zdravo_Budi[1100:207] setMessage should be triggered now
I expected that setMessage triggered !!! will be written every 3 seconds.

Any idea why this happens?

Thanks a lot!
DeeJay_CRO is offline   Reply With Quote
Old 05-27-2011, 06:38 AM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Code:
- (void)setMessageText: (NSTimer *) timer {
    NSLog(@"setMessage triggered !!!");
}
__________________
dany_dev is offline   Reply With Quote
Old 05-27-2011, 06:52 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DeeJay_CRO View Post
Hello!

Thanks for the answers. I did it in the following way:
Code:

- (void) thisMethodIsTrigered {
NSLog(@"setMessage should be triggered now");
    NSTimer *messageTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                    target: self
                                    selector:@selector(setMessageText:)
                                    userInfo:nil
                                    repeats:YES];
    

    [[NSRunLoop currentRunLoop] addTimer:messageTimer forMode:NSDefaultRunLoopMode];


    
}

- (void)setMessageText {
    NSLog(@"setMessage triggered !!!");
}
In the console, I see only:
2011-05-27 13:15:48.924 Zdravo_Budi[1100:207] setMessage should be triggered now
I expected that setMessage triggered !!! will be written every 3 seconds.

Any idea why this happens?

Thanks a lot!
That second line doesn't belong there (I marked it in bold red.) The method scheduledTimerWithTimeInterval creates a timer and adds to the default run loop all in one step. I don't know what will happen when you then add the timer again, but it probably won't be good.

Dany's pointed out the other problem that your setMessageText method needs to have the timer as a parameter. Methods with different numbers of parameters (and different parameter names) are actually different methods in Objective C.

The selector for a method with one parameter looks like this:

Code:
@selector(setMessageText:)
(note the colon after the method name)

The selector for a method with no parameters looks like this:

Code:
@selector(setMessageText)
(note the lack of a colon after the method name)

The selector for a timer method needs to take one and only one parameter: the timer itself.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 05-27-2011 at 06:55 AM.
Duncan C is offline   Reply With Quote
Old 05-27-2011, 07:17 AM   #7 (permalink)
Registered Member
 
DeeJay_CRO's Avatar
 
Join Date: Feb 2011
Location: Osijek, Croatia
Posts: 64
DeeJay_CRO is on a distinguished road
Default

Thanks for very detailed explanation. It works great!.

Kind regards!
DeeJay_CRO is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 343
6 members and 337 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0