My app performs a background fetch, in which it does a series of communications with a web service, and posts local notifications if there's some new information.
I have an umbrella method, called GetEverything, that starts this chain of logging in, getting, posting, processing data, etc. This is the method that is called upon a background fetch.
GetEverything can also be triggered by a user in the foreground. In that case it always works as expected.
When GetEverything is called from the background fetch, is gets stuck on individual parts of the GetEverything chain. Some of the spawned threads just hang. Only when I bring the app to the foreground, they resume.
To solve this, I forced GetEverything to a low-prio thread:
- (void)GetEverything:(void (^)(BOOL))completionHandler{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//my chain of events;
});
}
That helped a lot, but still, I had to get deeper into the chain, to pinpoint other methods, and force them on low priority as well.
One example of a method in my chain that pops back to the main thread is
-(NSString*) deleteHTMLfrom: (NSString*) htmlString {
if (![htmlString isKindOfClass:[NSNull class]]){
NSAttributedString* attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
NSString* plainText = [attributedText string];
return plainText;
}
return htmlString;
}
My question: how to I force all the 'links' of my chain to perform on a background thread?