I have a function which update pagination i.e. calculate total number of pages.
- (void) updatePagination{
if(!paginating){
NSLog(@"Pagination Started!");
paginating = YES;
totalPagesCount=0;
for (UIGestureRecognizer *gr in self.pageViewController.gestureRecognizers) {
[gr setEnabled:NO];
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self loadSpine:currentSpineIndex atPageIndex:currentPageInSpineIndex];
[[loadedEpub.spineArray objectAtIndex:0] setDelegate:self];
//This Line calls method of another class
[[loadedEpub.spineArray objectAtIndex:0] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];
[currentPageLabel setText:@"?/?"];
}
}
}
This is the method it calls
- (void) loadChapterWithWindowSize:(CGRect)theWindowSize fontPercentSize:(int) theFontPercentSize{
fontPercentSize = theFontPercentSize;
windowSize = theWindowSize;
UIWebView* webView = [[UIWebView alloc] initWithFrame:windowSize];
[webView setDelegate:self];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath]];
[webView loadRequest:urlRequest];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[webView dealloc];
}
- (void) webViewDidFinishLoad:(UIWebView*)webView{
NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
pageCount = (int)((float)totalWidth/webView.bounds.size.width);
[webView dealloc];
[delegate chapterDidFinishLoad:self];
}
Because of this my UI not get struck but when user dismiss or pop the view app get crashed. Also another button to increase and decrease the size of the text also called same method so if already it is running and user clicks on resize the font it struck or sometime get crashed.
My Questions are:
- Is it possible to run it in background and if user dismiss the modal then instead of crash it should stop.
- If user clicks on resizing of font size then it should stop old and run new.
- can I utilize the totalpages calculated even if its processing still going on.
I used
[self performSelectorOnMainThread:@selector(LoadChapters) withObject:nil waitUntilDone:NO];
(works but sometime give error for webview)
and NSOperation (not works)
to call that single line of function call.