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...
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;
And...
// Add to FBRequest.m
- (void)cancel {
[_connection cancel];
[_connection release], _connection = nil;
}
Now, to expose an interface in the Facebook class to make use of the new method...
// Add to Facebook.h
- (void)cancelPendingRequest;
And...
// 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…