Objective-C:线程中的服务器请求(如Android中的AsyncTask) [英] Objective-C: server requests in a thread (like AsyncTask in Android)

查看:76
本文介绍了Objective-C:线程中的服务器请求(如Android中的AsyncTask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动一个服务器请求,你可以取消。

I would like to start a server request, you can cancel.

我的想法是在一个线程中启动请求,这样用户界面就不会冻结。因此,您可以通过单击取消按钮来终止包括请求在内的整个线程。

My idea is to start the request in a thread so that the user interface does not freeze. So you can kill the whole thread including the request with a click on a "Cancel"-button.

使用Android它可以工作:服务器请求在AsyncTask中启动并且在onReturn() - 方法中,我可以在服务器请求完成后立即做出反应。

With Android it works: the server request gets started in a "AsyncTask" and in the "onReturn()"-method I can react as soon as the server request finish.

如何在iOS上使用Objective-C实现这一点?
我的第一次尝试是NSInvocationOperation。您可以取消操作,但在请求完成且结果可用时很难处理。我认为NSInvocationOperation不是我的问题的解决方案。

How can I implement this using Objective-C on iOS? My first attempt was a "NSInvocationOperation". You can cancel the operation, but it's difficult to handle when a request is completed and results are available. I think NSInvocationOperation is not the solution for my issue.

你会推荐给我吗? NSThread是我的正确选择吗?

The would you recommend to me? Is NSThread the right choice for me?

非常感谢!

推荐答案

注意!

是非常古老的答案现在仅用于历史目的。

精彩的ASIHttpRequest库不再存在;技术现在完全不同。

使用ASIHttpRequest执行此操作简直令人难以置信。

It is unbelievably simple to do this with ASIHttpRequest.

(异步是如此简单,没有理由你不会异步地执行它。)

(Asynchronous is so simple, there is no reason you would ever do it not-asynchronously.)

这里有一些粗略的可能会让你开始的提取。

Here are some rough extracts that might get you started.

...
ASIFormDataRequest *request;
...
NSURL *url = [NSURL URLWithString:@"https://blah.blah/blah.cgi?blah"];
request = [ASIFormDataRequest requestWithURL:url];

[request setPostValue:@"fred" forKey:@"username"];
[request setPostValue:@"flint" forKey:@"passie"];
[request setPostValue:@"stone" forKey:@"town"];

// send up data...
[request setData:[NSData dataWithBytes:blah length:blah] forKey:@"thefile"];

// or perhaps something like...
[request setData:imageData withFileName:@"blah.png"
        andContentType:@"image/jpeg" forKey:@"photoimage"];

[request setDelegate:self];
[request setDidFinishSelector:@selector(postingDone:)];
[request setDidFailSelector:@selector(postingDoneProblem:)];
[request startAsynchronous];
...

-(void) postingDone:(ASIHTTPRequest *)request
    {
    // it worked
    }
-(void) postingDoneProblem:(ASIHTTPRequest *)request
    {
    // failed
    }

真的不容易。您基本上只是输入字段和值。

Couldn't really be any easier. You're basically just typing out the fields and values.

根据您的问题,以下是取消飞行中请求的方法 ...只需将委托设置为nil,然后取消它。

Per your question, here is how you cancel an "in-flight" request... just set the delegate to nil and then "cancel" it.

  [myRequest setDelegate:nil];
  [myRequest cancel];
  [myRequest release];

ASIHttpRequest是奇迹库。如果您是iOS新手,ASIHttpRequest只是最常用的第三方库。从本质上讲,300,000个iPhone应用程序的每个iPhone应用程序都使用它。

ASIHttpRequest is the "miracle library". If you are new to iOS, ASIHttpRequest is simply THE most used 3rd party library. Essentially, every single iPhone app of the 300,000 iPhone apps uses it.

如果可能的话,一定要向这个人捐出几块钱 - 如果他停止支持那个图书馆,100,000名iPhone程序员受到了骚扰!

If at all possible BE SURE to donate a few bucks to the guy -- if he stops supporting that library, 100,000 iPhone programmers are buggered!

文档很简单,孩子可以关注它:

http://allseeing-i.com/ASIHTTPRequest/How-to-use

创建异步请求

the documentation is trivial, a child can follow it:
http://allseeing-i.com/ASIHTTPRequest/How-to-use
"Creating an asynchronous request"

它可能 - 几乎可以肯定 - 是任何平台上最令人惊讶的简单网络库。你快乐地做你所描述的事情是微不足道的。享受。

it is probably - almost certainly - the most amazingly simple networking library on any platform. It is trivial to do what you describe, happily. Enjoy.

这篇关于Objective-C:线程中的服务器请求(如Android中的AsyncTask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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