I wrote a simple Mac OS application. The view only contains a hidden WebView and a NSTextView. At a certain time 100 URLs will be parsed, one after another. A URL is loaded in the WebView, some DOM tree parsing happens, rinse and repeat.
Everything works fine except there are some major differences in loading times when the computer is in idle (no user activity). When i am using the computer a URL takes about 2 seconds to load. When i am not using the computer for some time, a URL takes about 1 minute to load.
When i come back to the computer after some time and see in the TextView that the last URLs took about a minute, the next ones immediately will be loaded in seconds. So it has to be something with the user activity on the system. Right? Anybody knows why?
I am on OS X 10.9.4.
Here is the code how i load the URLs:
#pragma mark -
- (void)processNextUrl
{
// No url, stop
if (_processQueue.count == 0)
return;
// Process url
NSString *urlString = [_processQueue objectAtIndex:0];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[_webView mainFrame] loadRequest:request];
}
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
// Page ready for parsing?
if ([[sender mainFrame] isEqual:frame] == NO)
return;
// Append date, time and url to TextView
NSString *url = [[[[frame dataSource] request] URL] absoluteString];
[self logText:url];
// Parser does some DOM parsing and outputs to TextView
Parser *parser = [[Parser alloc] initWithWebFrame:frame delegate:self];
[parser parse];
}
#pragma mark - ParserDelegate Methods
- (void)parser:(Parser *)parser didFinishParsingUrl:(NSString *)url
{
[_processQueue removeObjectAtIndex:0];
[self processNextUrl];
}
I'd bet AppNap is kicking in and starving your process of resources.
You may want to either disable AppNap in the Finder's Get Info window of your app - a temporary test to see if I'm right of course - or just use NSProcessInfo's beginActivityWithOptions:reason: to tell the system that the activity was user initiated and the app should not be throttled.