views:

332

answers:

3

Hi Everyone:

I am wondering if there is some way to run a function every minute in Cocoa. I, personally, will be using this for saving content as the user types it in case they quit - But I have seen this effect also used in Twitter clients. When the user keeps the window open, it will auto-update every x minutes without input from the user. It seems as if this is common, and the language allows it, I just can't seem to find documentation on it.

Thanks for any help!

+2  A: 

Check out the NSTimer docs. It does exactly what you want. You create an NSTimer that will repeat for as long as you want, and call a specific function with specified arguments.

Dan Lorenc
Hey Dan:So that's what NSTimer is for. I have looked into that class before, but didn't quite know that you could use it for functions. Thanks for your help.
PF1
NSTimer can't call a function directly; it can only send a message to an object. Your response to that message can be whatever you want, including calling a function. That said, I can't think of a reason why you would make this a function; you'd more likely just do the auto-save in the method.
Peter Hosey
+6  A: 

I, personally, will be using this for saving content as the user types it in case they quit - But I have seen this effect also used in Twitter clients.

A better solution would be to be the text view's delegate, and respond to textDidChange: by creating the non-repeating timer (if you have not done so already or it has already fired) and setting its fire date to X seconds in the future. Then, the user loses no more than X seconds' worth of work, not up to one minute, and the timer is not firing when the user has not typed anything.

Peter Hosey
Hey Peter:I will definitely look into this.
PF1
Peter: This seems to work very well, but it causes my application window to lag a lot when inputting text. I believe this is because it is saving the file to disk every time the function runs. I didn't implement the timer on this function, as I didn't quite know how to make it not create a new timer on each of the function's run. I imagine this would solve my problem, but I am not quite sure how to accomplish this. Any ideas?
PF1
When the timer fires, release it (if you retained it earlier) and set the variable holding it to `nil`. Then you can test in `textDidChange:` whether the timer exists or not. Also, don't call the function (or send the message) directly from `textDidChange:`; just create the timer, and let it send the message when it fires.
Peter Hosey
Remember, the idea is for the timer to fire X seconds *after* the user's last change. That's why your `textDidChange:` method should postpone the timer with every change.
Peter Hosey
+2  A: 

I, personally, will be using this for saving content as the user types it in case they quit

Then you want autosaving, which takes care of that for you.

Graham Lee
@Graham LeeWould this work even though my NSTextFields were dynamically added to the screen?
PF1
If the text they represent is part of the document model, then yes.
Graham Lee