Facebook API - 如何取消图形请求 [英] Facebook API - How to cancel Graph Request
问题描述
我偶尔需要取消一个 FaceBook 图形请求,但他们的 API 中似乎没有取消或类似的方法来这样做.目前,有时会发生崩溃,因为我分配给请求的委托已被解除分配.提交后有没有办法取消图形请求?
I occasionally need to cancel a FaceBook graph request, but there seems to be no cancel or similar method in their API to do so. At the moment, crashes sometimes occur as the delegate I assigned to the request has been deallocated. Is there any way to cancel a graph request once submitted please?
推荐答案
我假设您在谈论 facebook-ios-sdk 项目,并且 Facebook.h 中缺少取消方法.我也注意到了这一点,并最终决定添加我自己的取消方法.请注意,您分配给请求的委托不应该被释放然后引用,因为请求保留了委托.请参阅这个类似的问题.现在,如果你发现自己因为其他原因真的需要一个取消方法......
I'm assuming you're talking about the facebook-ios-sdk project, and the lack of a cancel method in Facebook.h. I noticed this as well, and eventually decided to add my own cancel method. Just to note, the delegate you assign to the request shouldn't ever be dealloc'd and then referenced, because the request retains the delegate. See this similar question. Now, if you find yourself really needing a cancel method for some other reason...
添加取消方法:
Facebook 请求是以不透明的方式提出的.您永远不会看到它们,只能通过 Facebook
类听到结果.在幕后,Facebook
类使用(非公开使用)FBRequest
类发出 Graph API 请求.这个类基本上是一个花哨的 NSURLConnection
委托.所以要取消请求,成员NSURLConnection
只需要被告知cancel
.将此方法添加到 FBRequest:
Adding a cancel method:
Facebook requests are made in an opaque manner. You never see them, and only hear about results via the Facebook
class. Under the hood, the Facebook
class makes Graph API requests with the (not for public use) FBRequest
class. This class is is basically a fancy NSURLConnection
delegate. So to cancel the request, the member NSURLConnection
just has to be told to cancel
. Adding this method to FBRequest:
// Add to FBRequest.h
- (void)cancel;
还有……
// Add to FBRequest.m
- (void)cancel {
[_connection cancel];
[_connection release], _connection = nil;
}
现在,要在 Facebook 类中公开一个接口以使用新方法...
Now, to expose an interface in the Facebook class to make use of the new method...
// Add to Facebook.h
- (void)cancelPendingRequest;
还有……
// Add to Facebook.m
- (void)cancelPendingRequest {
[_request cancel];
[_request release], _request = nil;
}
仅此而已.上述方法将取消最近的请求,您将永远不会再收到它的消息.
That's all there is to it. The method above will cancel the most recent request, and you'll never hear from it again.
这篇关于Facebook API - 如何取消图形请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!