取消已释放对象中的块请求
所以我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您应该能够调用
cancel
来停止操作。As far as I've seen, you should be able to call
cancel
to stop the operation.我做了一些示例代码,结果可能会让您感兴趣。
我创建了一个类,该类创建一个像您一样的请求:
并调用它:
我得到的结果是:
当在块内传递时,您的对象应该被保留。它应该避免这些崩溃。
无论哪种方式,正如 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:
And called this with:
The result that I got was:
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.