Facebook API - 如何取消图形请求

时间:2011-02-10 15:18:48

标签: ios facebook facebook-graph-api facebook-ios-sdk

我偶尔需要取消FaceBook图形请求,但似乎没有取消或类似方法在其API中这样做。此时,有时会发生崩溃,因为我分配给请求的委托已被解除分配。有没有办法在提交后取消图表请求?

9 个答案:

答案 0 :(得分:10)

我假设您正在谈论facebook-ios-sdk项目,以及Facebook.h中缺少取消方法。我也注意到了这一点,并最终决定添加我自己的取消方法。请注意,您分配给请求的委托不应该被释放然后被引用,因为请求会保留委托。见this similar question。现在,如果您因某些其他原因发现自己真的需要取消方法......

添加取消方法:
Facebook请求是以不透明的方式提出的。你永远不会看到它们,只能通过Facebook类听到结果。在引擎盖下,Facebook类使用(非公共用途)FBRequest类生成Graph API请求。这个类基本上是一个花哨的NSURLConnection委托。因此,要取消请求,只需要将成员NSURLConnection告知cancel。将此方法添加到FBRequest:

// Add to FBRequest.h
- (void)cancel;

和...

// Add to FBRequest.m
- (void)cancel {
    [_connection cancel];
    [_connection release], _connection = nil;
}

现在,要在Facebook类中公开一个接口以使用新方法......

// Add to Facebook.h
- (void)cancelPendingRequest;  

和...

// Add to Facebook.m
- (void)cancelPendingRequest {
    [_request cancel];
    [_request release], _request = nil;
}

这就是它的全部。上述方法将取消最近的请求,您将永远不会再听到它。

答案 1 :(得分:4)

我已经按照这里列出的Matt Wilding的方法,这非常有用,谢谢Matt。不幸的是它对我来说没什么用,所以我做了一些调整,现在它的工作原理......这个修改过的方法也不在核心的facebook类中......

//in .h define an FBRequest property
@property (nonatomic, retain) FBRequest * pendingFBRequest;

//in .m when making your request, store it in your FBRequest property
pendingFBRequest = [facebook requestWithGraphPath:@"me/feed"
                                        andParams:params
                                    andHttpMethod:@"POST"
                                      andDelegate:self];

//create a timer for your timeout
pendingFacebookActionTimer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(onPendingFacebookActionTimeout) userInfo:nil repeats:NO];

//cancel the action on the timeout's selector method
-(void)onPendingFacebookActionTimeout {

    [pendingFBRequest.connection cancel];

}

答案 2 :(得分:4)

2012年4月22日更新

我使用最新的Facebook iOS SDK更新Matt的版本。我的项目正在使用ARC,但我包含非ARC Facebook来源,以便我可以修改代码。 (当然,我们需要为Facebook源文件设置“-fno-objc-arc”标志)。棘手的部分是防止内存泄漏,我想我正在做的。但是当我在仪器中测试它时,我仍然看到非常少量的内存泄漏。幸运的是,细节显示它们与这些代码无关,所以我只是假设它们与应用程序资源处理有关。

以下是我实施的代码:

// Add to Facebook.h
- (void)cancelPendingRequest:(FBRequest *)releasingRequest; 

和...

// Add to Facebook.m
- (void)cancelPendingRequest:(FBRequest *) releasingRequest{
    [releasingRequest.connection cancel];
    [releasingRequest removeObserver:self forKeyPath:requestFinishedKeyPath];
    [_requests removeObject:releasingRequest];    
}

在您使用FBRequestDelegate

的项目中
// Declare this member or property to the .h file
FBRequest * currentFbRequest;

// Declare this method
-(void)cancelFBRequest;

而且......

// In .m file
AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// prepare your necessary request data and parameter ...
currentFbRequest = [appDelegate.facebook requestWithGraphPath:@"/me/photos" 
    andParams:params 
    andHttpMethod:@"POST" 
    andDelegate:self];



// Then in the method where you want to cancel
AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook cancelPendingRequest:currentFbRequest];
currentFbRequest=nil;

答案 3 :(得分:2)

对于我们这些构建静态库但无法访问实现文件的人来说,类别将是最佳方式。

对于我们这些没有构建静态库的人来说,使用类别也是最佳的,因为您不需要修改现有文件。

这是所说的类别。

// Facebook+Cancel.h
#import "Facebook.h"

@interface Facebook (Facebook_cancel)

- (void)cancelPendingRequest:(FBRequest *)releasingRequest;

- (void)cancelAllRequests;

@end

然后是.m文件

// Facebook+Cancel.m
#import "Facebook+Facebook_cancel.h"

@implementation Facebook (Facebook_cancel)

- (void)cancelPendingRequest:(FBRequest *)releasingRequest{
    [releasingRequest.connection cancel];
    if ([_requests containsObject:releasingRequest]) {
        [_requests removeObject:releasingRequest];
        [releasingRequest removeObserver:self forKeyPath:@"state"];

    }
}

- (void)cancelAllRequests {
    for (FBRequest *req in [_requests mutableCopy]) {
        [_requests removeObject:req];
        [req.connection cancel];
        [req removeObserver:self forKeyPath:@"state"];
    }
}

@end

对于那些使用任何其他答案的人,您导致内存泄漏。 Facebook SDK将通过NSLog警告您尚未删除观察者。 cancelAllRequests方法中的第四行修复了这个问题。

答案 4 :(得分:1)

试试这个而不是使用NSTimer:

FBRequest *fbRequest = [facebook requestWithGraphPath:@"me" andDelegate:self];
[self performSelector:@selector(fbRequestTimeout:) withObject:fbRequest afterDelay:30];

- (void)fbRequestTimeout:(FBRequest *)fbRequest
{
    [fbRequest.connection cancel];
    [fbRequest setDelegate:nil];
}

答案 5 :(得分:1)

自SDK 3.1起,它非常简单,因为startWithCompletionHandler:会返回一个FBRequestConnection对象,该对象具有-(void)cancel;方法。

例如:

// In interface or .h definitions:
@property (strong, nonatomic) FBRequest             *fBRequest;
@property (strong, nonatomic) FBRequestConnection   *fbConnection;

// when needed in class (params should be set elsewhere, this is just an example):
self.fBRequest = [[FBRequest alloc] initWithSession:[FBSession activeSession] graphPath:@"me/photos" parameters:params HTTPMethod:@"POST"];
self.fbConnection = [self.fBRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error){
    NSLog(@"Publish complete, error: %d", error.code);
}];

// now, to cancel anywhere in the class, just call:
[self.fbConnection cancel];

答案 6 :(得分:0)

FBRequest.h中,我不得不add _delegate = nil;因为在我的情况下,请求代理不再存在(它被解雇)导致崩溃。

答案 7 :(得分:0)

我之前的iOS Facebook SDK崩溃了,每当我导航到另一个视图时,它都会在2012年8月生效。我的解决方案基于@staticfiction响应:

在.h中添加了BOOL viewWillDisappear标记。在-(void) viewWillDisappear:中,将标志设置为YES。在-(void) viewDidAppear:

中将标记重置为NO
//in .h define an FBRequest property
@property (nonatomic, retain) FBRequest * pendingFBRequest;


 /*
 * Graph API: Search query to get nearby location.
 */
- (void)apiGraphSearchPlace:(CLLocation *)location {

    currentAPICall = kAPIGraphSearchPlace;
    NSString *centerLocation = [[NSString alloc] initWithFormat:@"%f,%f",
                                location.coordinate.latitude,
                                location.coordinate.longitude];
    JMYAppDelegate *delegate = (JMYAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"place",  @"type",
                                   centerLocation, @"center",
                                   @"1000",  @"distance",
                                   nil];
    [centerLocation release];
    pendingFBRequest = [[delegate facebook] requestWithGraphPath:@"search" andParams:params andDelegate:self];

    if (viewWillDisappear) {
        [pendingFBRequest.connection cancel];
        [pendingFBRequest setDelegate:nil];
        [self hideActivityIndicator];
    }
}

答案 8 :(得分:-2)

对此网址进行CURL调用

https://graph.facebook.com/REQUEST_ID?method=delete