访问网页时,阻止和不阻止有什么区别

发布于 2024-12-04 01:39:49 字数 1343 浏览 1 评论 0原文

我正在使用ASIHTTPRequest框架,在文档中,第二个有什么区别第三个例子,在用法优点缺点中?

第二个示例(创建异步请求):

- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];

// Use when fetching binary data
NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

第三个示例(使用块)

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      // Use when fetching text data
      NSString *responseString = [request responseString];

      // Use when fetching binary data
      NSData *responseData = [request responseData];
   }];
   [request setFailedBlock:^{
      NSError *error = [request error];
   }];
   [request startAsynchronous];
}

I am using ASIHTTPRequest framework, in the document, what are the differences between the 2nd and 3rd example, in usage, advantage and disadvantage?

2nd example (Creating an asynchronous request):

- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];

// Use when fetching binary data
NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

3rd example (Using blocks)

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      // Use when fetching text data
      NSString *responseString = [request responseString];

      // Use when fetching binary data
      NSData *responseData = [request responseData];
   }];
   [request setFailedBlock:^{
      NSError *error = [request error];
   }];
   [request startAsynchronous];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-12-11 01:39:49

iOS 中的块是并发编程的一部分

当您想要创建可以像一样传递的工作单元(即代码段)时,可以使用块。块通常用于编写回调

通常,使用块不会反映不同的应用程序行为。语法差异在于,使用块时,您不需要为异步请求定义请求委托或实现委托方法(例如 -requestFinished:-requestFailed:) 。

优点之一是可以在完成块中访问本地方法变量,因为块中的函数表达式可以引用并可以保留对本地变量的访问(例如方法中的变量url-grabURLInBackground: 或您的方法中定义的任何其他局部变量)。

第二个优点是使用嵌套请求调用。例如,您可能需要按顺序执行一些请求,如果没有块,您将需要为每个请求定义单独的委托方法回调,这可能会导致代码的可读性降低。

块允许您在调用点编写代码,稍后在方法实现的上下文中执行,当您习惯使用它们时,这可能非常有用。

Apple 阻止编程主题

The blocks in iOS are a part of Concurrent Programming

You use a block when you want to create units of work (that is, code segments) that can be passed around as though they are values. Blocks are usually used for writting a callbacks.

Usually, using blocks do not reflect in different applicatino behaviour. The syntactical difference is that, when using blocks you do not need to define a request delegate or implement delegate methods (such as -requestFinished: and -requestFailed:) for async requests.

One of the advantages is in accessing local method variables in completion block, bacause the function expression in block can reference and can preserve access to local variables (like variable url in your method -grabURLInBackground: or any other local variable defined in your method).

The second adventage is in using nested request calls. For example, you may need to perform a few requests in sequence, and without blocks you will need to define a separate delegate method callback for each request, which may result in reduced readability of your code.

Blocks allow you to write code at the point of invocation that is executed later in the context of the method implementation, which may be very usefull, when you get used to using them.

Some patterns to avoid when using blocks are mentioned in Apple Blocks Programming Topis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文