How to make a function been called every frame in the cocoa programming? - cocoa

I know cocoa application has a main run loop, how to make a function been called every frame? I mean this function should be called every process of the main loop. Is that through the -(void) run of the + (NSRunLoop*) currentRunLoop;?

You can call getCFRunLoop to get the Core Foundation RunLoop. Then it's just a matter of adding an observer to the CFRunLoop. See the docs for this function:
CFRunLoopAddObserver()

I'm assuming you are targeting OSX since the tag is cocoa and not cocoa-touch so if you want to synchronize with every screen frame update you should check out CVDisplayLink
I know it's been quite some time. Anyway this might come useful for someone who stumbled upon here like me.

Related

Cocoa: Entry point after view presented on the screen?

I'd like to start an operation as soon as the complete GUI of my OS X application has been presented on the screen.
My ViewController's "viewDidLoad()" is not being called in my case and "awakeFromNib()" seems to be called too early (my operations will be executed but no GUI is visible).
Reason for me to do that: I want to start some searching operations on application launch (without bothering the user to press a button)."awakeFromNib()" prevents the GUI from being shown which is very bad because the user does not know what's going on (searches may take a while).
Does anybody have a clue where I have to put my code in order to start it immediately AND let the GUI reflect the current operations?
I'm using Swift with Xcode Beta 4.
The usual way to do this is to start your long running operation on a background queue using either GCD or NSOperationQueues, This won't block your main UI thread. You then either have a delegate call-back or a block that runs when your operation is completed which lets you update your UI appropriately.

How to use sleep in qt creator

I am using Qt Creator C++. I want to see these two pictures after each other every 3 seconds. But according to my code, it shows just the last image on the screen. Does anybody have any idea what I am doing wrong?
You shall use QElapsedTimer() and implement a slot which you shall then connect to QElapsedTimer's signal that is emitted after desired time. See Qt help for more information on that.
In the slot, you shall implement your logic of showing the images.
I think that exec(); is the moment where program starts, so you have to change pictures after that. I Would pastle this code inside youre main class, or inside some other function (for example button slot).

Using GUI causes "Sleep?" making Audio stutter/stop

Okay, as Title says.
For example, i use NAudio to playback what i record (loopback if you want).
And if i click on the GUI (the top part, so i can move the window).
It will cause a "sleep", and when that happens the current activity (Audio playback) stops.
And then it continues afterwards.
But i want to remove that, as i don´t know any other application that has it, so it´s probably something to do with how i am programming.
Please keep it simple, i am extremely new to c#.
I am guessing on Bakckgroundworker or something, but i couldn´t get it to work.
So hopping for a more concrete answer.
This was just me not understanding that using the Main Thread in a window form will cause anything on the GUI to be run on it.
Meaning, if i move the GUI, that movement will be Priority over the rest of the code, so everything else will get paused if run on that thread.
Perhaps it differ from object to object, but in this scenario it was the case, so i just moved it to a separate thread and it´s solved.

How can I refresh core plot without returning from a method?

I have a cocoa interface that uses core plot. When I press a button in the interface, a plot is drawn. I wanted to create a sequence of graphs by calling the plotting method multiple times along with calls to sleep() in between. But it seems that even though the call to reload data is made that nothing happens until the function exits(only showing last graph as well). Now I know that CPAnimation exists but before I start using it I was wondering what it is that happens when the function exits that makes the graph refresh. Would I have to yield to the thread that takes care of the refreshing instead of using sleep?
Ok I figured out how. I called the reloadData method from a method in a separate thread (which always returns). This boiled down to calling reloadData from an IBAction and also with an NSTimer. Finally instead of using sleep I will use NSConditionLock coordinate the processing and the refreshing
Presumably, Core Plot (or your code) sets the view as needing display. That doesn't happen immediately; it happens when you return to the event loop.
Whenever you use sleep in a Mac OS X application, you kill a puppy. Use NSTimer instead. Have your timer callback method do the work required for one graph, and set whatever instance variables are necessary for your the method to know which graph it should draw, so that the method draws each graph in turn until it runs out of them.
Or, better yet, present a list of graphs that the user can choose from, instead of making the user watch all the graphs as a slide-show. (Unless an explicitly-labeled slide-show is what you're implementing.)
Core Plot, like most Cocoa drawing frameworks, is lazy: it draws at the end of the run loop iteration. This is to ensure that things aren't drawn too often.
Rather than drawing immediately, the layers are marked as needing drawing.
As was pointed out by others, a better approach to sleeping is to use NSTimer to avoid blocking the run loop, or to use NSObject methods like performSelector:withObject:afterDelay:
Peter has it right—the reload data method doesn't actually draw anything. The plot is marked as needing display and refreshed the next time the layers are drawn to the screen. If you use sleep, it never gets a chance to draw.
Also note that Core Plot is a fairly young project; CPAnimation and the related classes are stubs. They don't really do anything yet. :-)

RBSplitView has delayed reload of autosaved view positions

I really enjoy using RBSplitView, an open source replacement for NSSplitView, but I have a problem in my shipping app and am experiencing it again in a new project.
The problem is I'm telling the RBSplitView to autosave its position state by giving it an autosave name. When my app launches the RBSplitView doesn't seem to honor the saved state till a second after the window is drawn.
I've spent the night trying to debug the behavior but have had little success. Anyone out there use this lib and have some advice?
You can scrub this quicktime movie to the issue at work:
http://media.clickablebliss.com/billable/interface_experiments/rbsplitview_delayed_autosave_reload2.mov
I've still been unable to figure out why this is happening but I do have a workaround.
First, make sure your main window is not visible at launch and then at the end of applicationDidFinishLaunching in your app delegate add something like:
[mainWindow performSelector:#selector(makeKeyAndOrderFront:) withObject:self afterDelay: 0.1];
The delay is the key. If you just tell the window to makeKeyAndOrderFront: I still see the issue. However as long as it has a beat of time it looks good.
This likely is happening because the RBSplitView instance needs to wait until it's first moment to get to set its frame to the autosaved value, which happens to be after the user can see it. This 0.0-delay trick simply delays showing the window until the very next runloop, which gives the split view a chance to do its magic (and other views) so that when the user sees the window, it's already nice and sexy. So just do the delay at 0.0 and you'll be fine.
I have a similar, but slightly different workaround in my app that uses RBSplitView. In applicationDidFinishLaunching:, I call adjustSubviews on the split view before calling makeKeyAndOrderFront: on the window that contains it. This seems to knock the split view in to order before it gets displayed on the screen.

Resources