如何在 Objective C 中运行、重启和终止线程?

发布于 2025-01-08 11:41:21 字数 3417 浏览 0 评论 0原文

我有一个更新分页的功能,即计算总页数。

   - (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:@"?/?"];
        }
    }
}

这是它调用的方法,

- (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];

}

因此我的 UI 不会受到影响,但当用户关闭或弹出视图应用程序时,视图应用程序会崩溃。另外,另一个用于增加和减小文本大小的按钮也称为相同的方法,因此如果它已经在运行并且用户单击调整其所敲击的字体大小,或者有时会崩溃。

我的问题是:

  • 是否可以在后台运行它以及用户是否关闭模式 那么它应该停止而不是崩溃。
  • 如果用户单击调整字体大小,那么它应该停止旧的并运行新的。
  • 我可以利用计算出的总页数,即使其处理仍在进行中。

我使用

[self PerformSelectorOnMainThread:@selector(LoadChapters) withObject:nil waitUntilDone:NO]; (有效,但有时会出现 webview 错误)

和 NSOperation (无效)

来调用该单行函数调用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文