【问题标题】:setbackground loading with do while loop使用 do while 循环加载背景
【发布时间】:2014-03-27 18:18:50
【问题描述】:

我正在开发一个应用程序,我使用以下代码检查 url。

-(无效)存在 { NSString *strImg = @"一些网址"; NSURL *aImgUrl = [NSURL URLWithString:strImg]; NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:strImg]; [imgRequest setHTTPMethod:@"HEAD"]; imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; }

当我得到回复时,我会这样做。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([(NSHTTPURLResponse *)response statusCode] == 200) { // 网址存在 appDel.isExists = TRUE; 温度=假; [自加载数据]; NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); } 否则如果([(NSHTTPURLResponse*)响应状态码] == 404) { //url不存在 appDel.isExists = FALSE; 温度=真; [自加载数据]; NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); } }

这是我的加载数据代码

-(无效)加载数据 { 结果=假; isReqSent = 假; 做 { 如果(appDel.isExists == TRUE) { NSURL *aTxtUrl = [NSURL URLWithString:apiText]; NSURL *aImgUrl = [NSURL URLWithString:apiImage]; NSURLRequest *imgRequest = [NSURLRequest requestWithURL:aImgUrl]; NSURLRequest *txtRequest = [NSURLRequest requestWithURL:aTxtUrl]; [NSURLConnection sendAsynchronousRequest:txtRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 如果(数据) { NSString *strTxt = [[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding]; [lblTime setText:strTxt]; [policyImgWebView loadRequest:imgRequest]; 结果=真; } }]; } 如果(结果) { appDel.isExists = TRUE; } 别的 { 如果(!isReqSent) { NSString *soapMessage = @"我的肥皂信息"; NSString *soapAction = @"我的肥皂网址"; [objWebServiceParser xmlParsing:soapMessage:soapAction:Number]; isReqSent = TRUE; } } 如果(温度) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [自背景进程]; }); } } 而(结果 == FALSE);

这是我的后台进程

-(void)backgroundProcess { NSString *strImg = @"一些网址"; NSURL *aImgUrl = [NSURL URLWithString:apiImage]; NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl]; [imgRequest setHTTPMethod:@"HEAD"]; imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; }

我不知道我哪里做错了,谁能指导我?

我希望后台进程继续运行,直到它获取数据,当接收到的数据 appdel.isExists 为真并进入主线程以更新 ui。

我尝试了 GCD 和 performSelectorInTheBackground,但我没有做对,我得到 NSThread 失败错误 35。


它与 NSURLSessionDownloadTask 一起工作,但仍在寻找更好的选择。

【问题讨论】:

  • 你的意思是 performSelectorInTheBackground ?
  • 是的,当我处理到后台时,我在 NSURLConnection 委托方法中没有得到响应。
  • 您只想从 URL 加载图像吗?
  • 是的@AkshitZaveri,但在后台(继续检查直到 url 有效,一旦有效加载它)

标签: ios objective-c nsxmlparser nsthread nsurlconnectiondelegate


【解决方案1】:

不要在后台调用NSURLConnection。因为NSURLConnection 会自动工作。由于您没有发送异步请求,因此它将在后台工作并且不会挂起您的主线程。在您的加载数据中删除调度。你有:

if (temp)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [self backgroundProcess];
        });
    }

制作:

if (temp)
    {
        [self backgroundProcess];
    }

你不需要使用 dispatch。

【讨论】:

  • 然后做while循环会消耗大量内存
  • 内存和后台执行任务没有任何关系。记忆是完全不同的东西。你在使用 ARC 吗?
【解决方案2】:

要从服务器加载UIImageView 中的图像,您可以使用以下框架。

SDWebImage

您将获得 completionBlockprogressBlock 以及更多可用于更新 UI 的属性。它会分块通知您,然后您可以在 UI 上更新您想要的任何内容。

示例:

如果imageURL 有效(这是你的情况),只需加载图像就像这段代码一样简单

UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]];

而且它也有如下丰富的方法

UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL] 
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
           {
               // in "image" you'll get the downloaded image from the URL
               ... completion code here ...

           }];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2021-07-18
    • 2013-02-16
    • 1970-01-01
    • 2018-05-09
    • 2015-07-03
    • 2016-02-21
    相关资源
    最近更新 更多