简体   繁体   English

从iPhone中的服务器加速响应检索器

[英]Speed up the response retriever from server in 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
}

The problem with this code is ,If server shows lag and it takes lets say 3 seconds to get this response 这段代码的问题是,如果服务器显示延迟并且需要花费3秒才能得到此响应

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

For 3 seconds my iPhone screen is jammed. 3秒钟我的iPhone屏幕被卡住。 How can i make it run in background so that it won't slow down or jam the mobile 我如何使其在后台运行,以免减慢移动速度或阻塞手机

Regards 问候

What you are doing is sending HTTP request from the main thread. 您正在做的是从主线程发送HTTP请求。 that will jam up the UI as you said. 如您所说,这会阻塞UI。 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. 您需要生成一个后台线程并向您的服务器发出请求,当响应返回时,您需要从主线程更新UI。 This is a common pattern in UI coding. 这是UI编码中的常见模式。

__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];
        }
    });
});

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 . 您还可以使用performSelectorInBackground:withObject:生成一个新线程,然后执行的选择器负责设置新线程的自动释放池,运行循环和其他配置详细信息–请参阅Apple的《 线程编程指南 中的“使用NSObject生成线程” 。 。

You'd probably be better off using Grand Central Dispatch as I posted above. 如我上面所述,使用Grand Central Dispatch可能会更好。 GCD is a newer technology, and is more efficient in terms of memory overhead and lines of code. GCD是一种较新的技术,在内存开销和代码行方面更为高效。

您可以使用ASIHTTPRequest ,这是我最喜欢的获取和发送信息的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM