Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [129]

By Root 913 0

{

@autoreleasepool {

NSArray *results = nil;

// do background work ...

// the thread will end once this method returns,

// so let's report our results:

[self performSelectorOnMainThread:@selector(updateWithResults:)

withObject:results

waitUntilDone:NO];

}

}

- (void)updateWithResults:(NSArray *)theResults

{

[self setResults:theResults];

[tableView reloadData];

}

The method performSelectorOnMainThread:withObject:waitUntilDone: is very similar to performSelectorInBackground:withObject: in form but has an interesting additional argument: waitUntilDone. The waitUntilDone argument optionally makes this method block until the selector on the main thread has completed. In this case, it is not needed; the withObject: parameter (results) is retained by this method until after the selector is performed.

Note that we added an autorelease pool to our thread body by using @autoreleasepool. Whenever you create a background thread, you must supply your own autorelease pool.

Improving Scattered: Time Profiling in Instruments


Open the Scattered project from the previous chapter and find addImagesFromFolderURL: in ScatteredAppDelegate. Find the allowedFiles variable, which is used to limit the number of files opened to ten. Remove all traces of allowedFiles and run the application.

You should see an unresponsive window for several seconds, followed by the images appearing and animating. It looks like loading the images is blocking the main thread, which is a poor user experience. We have a pretty good idea that the problem is related to loading images, since we just removed the limit, but let’s prove it to ourselves.

Introducing Instruments


Instruments is a tool for analyzing a running program. The tool has many different plug-ins, called instruments, that enable you to look at various aspects, usually performance related, of the running application.

In Xcode, open the Product menu and select Profile. Xcode will use the Release build configuration (configurable in the Scheme Editor) to rebuild the project and will then start Instruments. Under Mac OS X, CPU in the template chooser, select Time Profiler and click Profile (Figure 34.2). Instruments will then run Scattered. Once the images have animated, click the red Stop button in Instruments to stop profiling.

Figure 34.2. Choose the Time Profiler in Instruments

The Instruments interface has several parts. The key ones are the Instruments list at the upper left; in this case, only one instrument, Time Profiler, is being run. To the right is the track pane, which displays graphical data over time related to each of the instruments being run. Looking at the graph for the Time Profiler, we can see that there was a fair burst of activity, probably related to loading the images. Below the track is the detail pane, which displays tabular data related to the selected instrument (Figure 34.3).

Figure 34.3. Running Scattered under the Time Profiler

Time Profiler works by taking snapshots of the application’s call stack repeatedly while it is running. This enables us to tell where the application is spending its time but does not, however, tell us how many times a particular method has been called, as it has only been taking snapshots and does not know when methods are entered and exited.

In the detail pane, we see the symbols sorted by time spent. Note the disclosure triangles in the Symbol Name column. Try clicking through them to navigate the stack, or use the keyboard arrow keys to explore the tree if you prefer. Note that Invert Call Tree is checked by default; this means that we are seeing the deepest functions where the CPU spent most of its time. You can toggle Invert Call Tree off to see the individual entry points to the application. Sometimes, this will make it faster to find the information you need.

If you dig down far enough, you’ll see that all this time is being spent in thumbImageFromImage: (a quick way to find this is to enable Hide System Libraries). If you show the Extended Detail pane (using the View segments

Return Main Page Previous Page Next Page

®Online Book Reader