【问题标题】:Speed up the response retriever from server in iphone加快来自 iphone 中服务器的响应检索器
【发布时间】:2013-01-23 08:17:28
【问题描述】:

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

        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 秒才能获得此响应

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

我的 iPhone 屏幕卡住了 3 秒。我怎样才能让它在后台运行,这样它就不会变慢或卡住手机

问候

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch background-thread


    【解决方案1】:

    您正在做的是从主线程发送 HTTP 请求。正如你所说,这会堵塞用户界面。您需要生成一个后台线程并向您的服务器发出请求,当响应返回时,您需要从主线程更新 UI。这是 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];
            }
        });
    });
    

    您也可以使用performSelectorInBackground:withObject: 生成一个新线程,然后执行的选择器负责设置新线程的自动释放池、运行循环和其他配置细节——请参阅 Apple 的 Threading Programming Guide 中的"Using NSObject to Spawn a Thread"

    您可能最好使用我上面发布的Grand Central Dispatch。 GCD 是一种较新的技术,在内存开销和代码行数方面效率更高。

    【讨论】:

    • 你能举一个例子来说明如何在我的代码中使用你的代码吗?那太好了,因为我还在学习过程中:)
    • 我想知道我是否将它转移到一个比方说 NSObject 类并从我的 UIViewController 调用它,但是当我这样做时,它不起作用。我想这样做是因为我必须在很多地方使用它,大代码,所以我只是传递 url 并返回响应?
    【解决方案2】:

    您可以使用我最喜欢的ASIHTTPRequest 来获取和发送信息。

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2011-01-26
      • 2023-04-11
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多