取消已释放对象中的块请求

发布于 2024-12-08 21:34:50 字数 624 浏览 0 评论 0原文

所以我使用 AFNetworking 通过网络服务执行异步请求。

AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON)
    {
        [self doSomeStuff];

    } failure:^(NSHTTPURLResponse *response, NSError *error)
    {
        XLog("%@", error);
    }];

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];

现在,当“self”对象在请求完成之前被释放时会发生什么,我的应用程序当然会在 [self doSomeStuff];<​​/code> 上崩溃。

在释放我的对象时有没有办法取消这个块请求?

so i'm using AFNetworking for doing asynchronous requests with web-services.

AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON)
    {
        [self doSomeStuff];

    } failure:^(NSHTTPURLResponse *response, NSError *error)
    {
        XLog("%@", error);
    }];

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
    [queue addOperation:operation];

now what happens when the "self" object is dealloced before the request finished is, that my application chrashes on [self doSomeStuff]; of course.

is there a way to cancel this block-request when deallocating my object?

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

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

发布评论

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

评论(2

停顿的约定 2024-12-15 21:34:50

据我所知,您应该能够调用 cancel 来停止操作。

As far as I've seen, you should be able to call cancel to stop the operation.

岁月苍老的讽刺 2024-12-15 21:34:50

我做了一些示例代码,结果可能会让您感兴趣。

我创建了一个类,该类创建一个像您一样的请求:

@implementation Aftest
@synthesize name = _name;
- (void) doSomeStuff
{
    NSLog(@"Got here %@", self.name);
}

- (void)startDownload
{   
      self.name = [NSString stringWithFormat:@"name"];
      NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2"];
      NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
      AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON)
      {
           [self doSomeStuff];

      } failure:^(NSHTTPURLResponse *response, NSError *error)
      {
           NSLog(@"%@", [error localizedDescription]);
      }];

      NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
      [queue addOperation:operation];
}
@end

并调用它:

Aftest *af = [[Aftest alloc] init];
NSLog(@"1 - retain count %d", [af retainCount] );
[af startDownload];
NSLog(@"2 - retain count %d", [af retainCount] );
[af release];
NSLog(@"3 - retain count %d", [af retainCount] );

我得到的结果是:

2011-10-09 09:28:41.415 aftes[6154:f203] 1 - retain count 1
2011-10-09 09:28:41.418 aftes[6154:f203] 2 - retain count 2
2011-10-09 09:28:41.419 aftes[6154:f203] 3 - retain count 1
2011-10-09 09:28:43.361 aftes[6154:f203] Got here name

当在块内传递时,您的对象应该被保留。它应该避免这些崩溃。

无论哪种方式,正如 Micheal 回答的那样,只要您有权访问操作对象,就应该可以调用 cancel

I did some sample code and the result can interest to you.

I created a class that creates a request just like yours:

@implementation Aftest
@synthesize name = _name;
- (void) doSomeStuff
{
    NSLog(@"Got here %@", self.name);
}

- (void)startDownload
{   
      self.name = [NSString stringWithFormat:@"name"];
      NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2"];
      NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
      AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON)
      {
           [self doSomeStuff];

      } failure:^(NSHTTPURLResponse *response, NSError *error)
      {
           NSLog(@"%@", [error localizedDescription]);
      }];

      NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
      [queue addOperation:operation];
}
@end

And called this with:

Aftest *af = [[Aftest alloc] init];
NSLog(@"1 - retain count %d", [af retainCount] );
[af startDownload];
NSLog(@"2 - retain count %d", [af retainCount] );
[af release];
NSLog(@"3 - retain count %d", [af retainCount] );

The result that I got was:

2011-10-09 09:28:41.415 aftes[6154:f203] 1 - retain count 1
2011-10-09 09:28:41.418 aftes[6154:f203] 2 - retain count 2
2011-10-09 09:28:41.419 aftes[6154:f203] 3 - retain count 1
2011-10-09 09:28:43.361 aftes[6154:f203] Got here name

Your object should be retained when passed inside the block. It should be avoiding these crashes.

Either way, as Micheal answered, it should be possible to call cancel as long as you have access to the operation object.

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