Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X: what causes the spinning beach ball?

When does OS X decide to give your app a spinning beach ball? What can you do when programming an application to avoid this?

like image 227
Mark Harrison Avatar asked Jan 18 '09 05:01

Mark Harrison


People also ask

Why do I keep getting the spinning beach ball on my Mac?

The ball signifies that your Mac cannot handle all the tasks given to it at this moment. Every app on your Mac has a so-called window server. When an app receives more events than it can process, the window server automatically shows you the spinning ball.

Why is there a spinning globe on my Mac?

A globe with an exclamation point means that your Mac tried to start up from macOS Recovery over the internet, but couldn't. Learn what to do if your Mac can't start up from macOS Recovery.


3 Answers

The window server will show the spinning wait cursor when the frontmost application, or the application that has a window under the mouse pointer, has not responded to events from the window server within a certain window of time.

To avoid the spinning wait cursor, an application needs to service events in a timely fashion. There's no way around this window server behavior, and for good reason: Applications on Mac OS X aren't ever supposed to be unresponsive to the user.

like image 189
Chris Hanson Avatar answered Sep 25 '22 20:09

Chris Hanson


The reason is that your app is blocking the UI. As the other posters said, the window manager can notice that you haven't handled events in awhile and put up this UI.

Most likely you are doing some IO (such as reading or writing to the disk, or doing network requests) synchronously on the UI (default) thread. A good rule of thumb to keeping your app responsive (and thus avoiding the beachball) is to never do synchronous IO on the UI thread. You can either use asynchronous IO (APIs that take a callback, do work on a background thread, and then notify you on the UI thread when they are done), or else you can use a separate background thread to do the work.

If you aren't doing IO, then you are probably in some kind of long loop on the UI thread that is causing you to be unresponsive. In that case, either optimize or remove the loop, or move it to a background thread.

like image 37
Aaron Boodman Avatar answered Sep 21 '22 20:09

Aaron Boodman


Assuming your application has sufficient hardware resources (which won't always be the case in reality), there's really no reason your app should ever beachball. If it does, figure out what section of code is blocking the user interface (if it's non-intuitive Shark.app will come in handy) and move that to a background thread (or use another strategy to eliminate the pause). Fortunately Cocoa and Objective-C have pretty good tools for threading, see NSOperationQueue to start with. Apple also has a few good documents on performance tuning, see this question for relevent links.

like image 42
Marc Charbonneau Avatar answered Sep 23 '22 20:09

Marc Charbonneau