我可以在基本的“基础"中建立异步NSURL连接吗?目标C文件? [英] Can I make an asynchronous NSURL Connection in a basic, "foundation" objective C file?

查看:116
本文介绍了我可以在基本的“基础"中建立异步NSURL连接吗?目标C文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Objective-C的基础知识,虽然语法已经毕业,但是我仍然没有做任何特定于ios的工作.无论如何,我试图找出http请求,但是我无法获得异步发布才能正常工作.

I am working through through the basics of Objective-C, I've graduated past syntax but I'm still not doing any ios specific stuff. Anyway, I'm trying to figure out http requests, and I can't get an asynchronous post to work correctly.

我可以做的很基础(这可行):

I can go very basic (this works):

NSString *doc = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];

NSLog(@"%@", doc);

我可以发出一个同步请求(也可以):

And I can make a synchronous request (also works):

NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

NSString *doc = [[NSString alloc]initWithData:urlData encoding:NSASCIIStringEncoding];

但是当我尝试这样做时:

But when I try this:

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];

我似乎无法触发任何连接方法,例如didReceiveResponse或didReceiveData.

I can't seem to get any of the connection methods to fire, such as didReceiveResponse or didReceiveData.

这是我的问题:

我要在命令行对象c应用程序中尝试做什么?

Will what I am trying to do work in a command-line obejctive c app?

-代表需要为自我"吗?我可以制作一个自定义的委托"类并添加诸如以下的方法吗?

-Does the delegate need to be "self"? Can I make a custom "delegate" class and add methods like:

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Data");
}

-如果可以使用自定义委托,这是一个好主意吗?我应该将上面的didReceiveData方法附加到正在执行连接的任何类上吗?

-If a custom delegate is possible, is it a good idea? Should I attach the above didReceiveData method to whatever class is performing the connection?

-我是否需要使某种任务连续运行,以使我的程序在响应出现之前不会结束?

-Do I need to have some kind of task running continuously so my program doesn't end before the response comes in?

-我完全把错误的树树成树了吗?你能使我挺直吗?

-Am I barking up the wrong tree entirely? Can you set me straight?

提前感谢您的宝贵时间.

Thanks in advance for your time.

推荐答案

NSURLConnection和NSURLRequest都是Foundation框架的一部分,因此您可以在程序中使用它们.但是,根据NSURLConnection文档:

NSURLConnection and NSURLRequest are both part of the Foundation framework, so you should be fine using them in your program. However, per the NSURLConnection documentation:

为使连接正常工作 调用线程的运行循环必须是 在默认运行循环中运行 模式.

For the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode.

您将要阅读有关 NSRunLoop .您的命令行应用程序可能没有运行循环,这就是为什么您的异步请求未达到预期效果的原因.

You'll want to read about NSRunLoop. Your command line app probably doesn't have a run loop, which is why your async request isn't doing what you expect.

委托不必一定是自我"的,它可以是您喜欢的任何对象,只要该对象实现了适当的委托方法.

The delegate doesn't have to be 'self' -- it can be any object you like provided that object implements the appropriate delegate methods.

很明显,如果程序在连接完成其工作之前退出,则您的程序将无法获取其正在寻找的数据.

Obviously, if the program exits before the connection completes its work, your program isn't going to get the data it was looking for.

这篇关于我可以在基本的“基础"中建立异步NSURL连接吗?目标C文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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