1

I have a cocoa application window (NSWindow) which position on the screen should be updated frequently (depending on some calculation). As noticed in the documentation, UI changes should be made on the main thread:

void calculationThread()
{
    while(true)
    {
        calculatePosition();
        if(positionChanged)
        {
            dispatch_async(dispatch_get_main_queue(), ^{ setWindowPos(); });
        }
    }
}

void setWindowPos()
{
    [window setFrame:_newFrame display:YES];
} 

Now the problem I have is that the window movement is very slow and delayed. After making some profiling I see that the calculation process takes about 40mSec, meaning that I'm queueing up a backlog of UI updates 25 times a second. I've read here that this might be faster than they can be processed and timer should be used to fire the changes every tenth of a second or so. But, wouldn't it be too slow for the human eye (I mean, in that case the movement wouldn't be delayed but would be lagged causing pretty much the same affect). I will appreciate some knowledge sharing on this. Actually my main 2 questions are:

  1. Are 25-30 UI updates per second really to much?
  2. If yes, what is the recommended UI changes frequency?
Community
  • 1
  • 1
Sanich
  • 1,739
  • 6
  • 25
  • 43
  • why do you need to constantly update window's frame? I think that can be achieved by updating it only when necessary (when `_newFrame` is assigned) – medvedNick Oct 23 '15 at 21:59
  • @medvedNick Its position being updated only when needed (if _newFrame as assigned) – Sanich Oct 23 '15 at 22:01
  • how could I ever miss that `if` statement? :) sorry then, I have nothing to say, except that from my experience you should dig into update frequency only when you see the window lagging, so, if 10 times per second is too slow, leave your frequency – medvedNick Oct 23 '15 at 22:09

2 Answers2

1

The frequency at which a window can be moved around onscreen without problems will of course depend upon the speed of the user's machine, the video card they have, the size of the window, and probably a bunch of other factors. There is no single good answer to this. However, if you just drag a window around on your screen, you will notice that it can probably be moved very smoothly (unless your machine is very busy or very low on memory or something); I would not expect 25 times per second to produce a problem on a modern Mac. Not even close, in fact.

@RobNapier's points about Core Animation etc. are fine, but overstated I think; there is nothing inherently wrong with changing your UI using a timer or other periodic update if that is what you actually want to do. CoreAnimation is a toolkit for making some types of animation easier; using it is not required, and it is not suited to every problem. Similarly, if you want to make changes that are actually synched to screen refresh then CVDisplayLink is useful, but it doesn't really sound like that's what you want to do.

For your purposes, your basic approach seems fine, although I would suggest adding an NSDate check in order to skip updates if the previous update was less than, say, 1/60th of a second previous. After all, the calculation appears to take 40mSec on your machine, but it might be much faster on some other machine; you want to throttle your drawing to a reasonable rate just to be a good citizen.

So what is the problem, then? I suspect the issue might actually be your call [window setFrame:_newFrame display:YES]. If you look at Apple's docs for that method, they state "When YES the window sends a displayIfNeeded message down its view hierarchy, thus redrawing all views." Each time you call that method, then, you are not only moving your window (which I gather is your intention); you are redrawing all of the contents of the window, too, and that is slow. If you don't need to do that, then that is the overhead you need to eliminate. Call setFrameOrigin: or setFrameTopLeftPoint: instead (which make the semantics clear, that you are moving the window without resizing it or redrawing it), or perhaps just setFrame:display: passing NO instead of YES, and I'm guessing your performance problem will vanish.

If you do in fact need to redraw the window contents every time, then please edit the problem description to reflect that. In that case, the solution will have to involve profiling why your window drawing is slow, and figuring out ways to optimize that, which is an entirely different problem.

bhaller
  • 1,803
  • 15
  • 24
  • Call `setFrameOrigin:` or `setFrameTopLeftPoint:` doesn't improve the smoothness and it remains laggy. – Sanich Oct 25 '15 at 14:23
  • OK, profile it and add the results to the question, then, so that we can see where the slowdown is. Moving a window around should not be that laggy. – bhaller Oct 25 '15 at 15:13
0

As you've discovered, you should never try to drive the UI from a tight loop. You should let the UI drive you. There are three primary tools for that.

For simple problems, AppKit is capable of moving windows around the screen. Just call [NSWindow setFrame:display:animate:]. You can override animationResizeTime: to modify the timing.

In many cases AppKit doesn't give enough control. In those case, the best tool is almost always Core Animation. You should tell the system using Core Animation how you where you want UI elements to wind up, and over what period and path, and let it do the work of getting them there. See the Core Animation Programming Guide for extensive documentation on how to use that. It focuses on animating CALayer, but the techniques are similar for NSWindow. You'll use [NSWindow setAnimations:] to add your animation. Look at the NSAnimatablePropertyContainer protocol (which NSWindow conforms to) for more information. For a simple sample project of animating NSWindow, see Just Say No from CIMGF.

In a few cases, you really do need to update the screen manually at the screen update frequency. I must stress how rare this situation is. In almost all cases, Core Animation is the correct tool. But in those rare case (some kinds of video for instance), you can use a CVDisplayLink to handle this. That will call you each time the screen would like to refresh, giving you an opportunity to update your content to match.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610