Frage

On Mac OS X, Cocoa is event driven. By this I mean each thing is driven by the UI - the user clicking something or moving over a certain area results in an event handler being called. Main simply calls NSApplicationMain () which creates an infinite loop till the program exits.

What about tasks that aren't user driven though? Where would I put tasks that need to work in the background to provide the basic functionality of the program? For example, Photo Booth does certain things when the user interacts with the UI (i.e. changes to video camera when user click on a certain button). However, Photo Booth is also continuously retrieving frames from the camera to provide a video stream.

So my question is where do non-UI tasks code go? Main() isn't really a possibility since I need the tasks to run throughout the life of the program rather than when the program just starts.

I'm on Mac OS X Snow Leopard (I want something that works on all Mac Systems though: i.e. Lion OS X) and am using a Obj-C Cocoa application in Xcode.

War es hilfreich?

Lösung

Any application cocoa based Application has access to using RunLoops.

Any work your app would want to do, would make use of the RunLoop to execute. Typically this is done on other threads, but if the work is lightweight enough not to cause your UI thread to block, you can run your code on the Main threads RunLoop.

I recommend reading Apple's Thread Programming Guide for more info.

With Grand Central Dispatch, the programming models have changed to make use of today's multicore systems, so it's possible to define a task and have the OS manage it's execution.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top