加快来自iPhone中服务器的响应检索器 [英] Speed up the response retriever from server in iphone

查看:53
本文介绍了加快来自iPhone中服务器的响应检索器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从服务器获取结果

I am using following code to get results from server

        NSString *queryString = @"MyString"

        NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];

        NSLog(@"%@",response);

        if (err != nil)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Error"
                                                           message: @"An error has occurred. Kindly check your internet connection"
                                                          delegate: self
                                                 cancelButtonTitle:@"Ok"
                                                 otherButtonTitles:nil];
            [alert show];
            [indicator stopAnimating];
        }
        else
        {
//BLABLA
}

此代码的问题是,如果服务器显示延迟,并且要花3秒才能得到此响应

The problem with this code is ,If server shows lag and it takes lets say 3 seconds to get this response

NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] 

3秒钟我的iPhone屏幕被卡住.我该如何使其在后台运行,以免降低速度或阻塞手机

For 3 seconds my iPhone screen is jammed. How can i make it run in background so that it won't slow down or jam the mobile

致谢

推荐答案

您正在做的是从主线程发送HTTP请求.如您所说,这会阻塞UI.您需要生成一个后台线程并向您的服务器发出请求,当响应返回时,您需要从主线程更新UI.这是UI编码中的常见模式.

What you are doing is sending HTTP request from the main thread. that will jam up the UI as you said. You need to spawn a background thread and make a request to your server, when the response comes back then you need to update the UI from the main thread. This is a common pattern in UI coding.

__block__  NSString *response;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    //your server url and request. data comes back in this background thread
    response; = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];

    dispatch_async(dispatch_get_main_queue(), ^{
        //update main thread here.
        NSLog(@"%@",response);

        if (err != nil)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Error"
                                                           message: @"An error has occurred."
                                                          delegate: self
                                                 cancelButtonTitle:@"Ok"
                                                 otherButtonTitles:nil];
            [alert show];
            [indicator stopAnimating];
        }
    });
});

您还可以使用 performSelectorInBackground:withObject:生成新线程,然后执行的选择器负责设置新线程的自动释放池,运行循环和其他配置详细信息-请参见《 Apple的线程编程指南》 中的使用NSObject生成线程" .

You can also use performSelectorInBackground:withObject: to spawn a new thread, then the performed selector is responsible for setting up the new thread's autorelease pool, run loop and other configuration details – see "Using NSObject to Spawn a Thread" in Apple's Threading Programming Guide.

使用

You'd probably be better off using Grand Central Dispatch as I posted above. GCD is a newer technology, and is more efficient in terms of memory overhead and lines of code.

这篇关于加快来自iPhone中服务器的响应检索器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆