首页 文章

setbackground加载与while循环

提问于
浏览
3

我正在使用以下代码检查网址的应用程序 .

-(void)exists
{
    NSString *strImg = @"some url";


    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)
    {
        // url exists
        appDel.isExists = TRUE;
        temp = FALSE;
        [self loadData];
        NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
    }
    else if([(NSHTTPURLResponse*)response statusCode] == 404)
    {
        //url does not exists
        appDel.isExists = FALSE;
        temp = TRUE;
        [self loadData];
        NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
    }
}

这是我的加载数据代码

-(void)loadData
{


    result = FALSE;

    isReqSent = FALSE;

    do
    {
        if (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)
             {
                 if (data)
                 {
                         NSString *strTxt = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                         [lblTime setText:strTxt];
                          [policyImgWebView loadRequest:imgRequest];

                         result = TRUE;                     
                 }

             }];

        }

        if (result)
        {
            appDel.isExists = TRUE;
        }
        else
        {
            if(!isReqSent)
            {
                NSString *soapMessage = @"my soap message";

                NSString *soapAction = @"my soap url";


                [objWebServiceParser xmlParsing:soapMessage :soapAction :Number];
                isReqSent = TRUE;
            }

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

    } while (result == FALSE);

这是我的后台流程

-(void)backgroundProcess
{

    NSString *strImg = @"some url";

    NSURL *aImgUrl = [NSURL URLWithString:apiImage];

    NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl];
    [imgRequest setHTTPMethod:@"HEAD"];

    imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self];

}

我不知道我哪里做错了,任何人都可以指导我吗?

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

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


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

2 回答

  • 0

    要从服务器加载 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 ...
    
               }];
    
  • 2

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

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

    做了:

    if (temp)
        {
            [self backgroundProcess];
        }
    

    您不需要使用派遣 .

相关问题