Facebook API – 如何取消graphics请求

我偶尔需要取消一个FaceBookgraphics请求,但是在他们的API中似乎没有取消或类似的方法。 目前,当我分配给请求的委托被释放时,有时会发生崩溃。 有没有办法取消一次提交图表请求?

我假设你正在谈论facebook-ios-sdk项目,并且在Facebook.h中缺less取消方法。 我也注意到了这一点,最终决定添加我自己的取消方法。 需要注意的是,您分配给请求的委托不应该被解除分配,然后被引用,因为请求保留了委托。 看到这个类似的问题 。 现在,如果你发现自己确实需要一个取消的方法由于其他原因…

添加取消方法:
Facebook请求以不透明的方式进行。 你永远不会看到他们,只能通过Facebook类听到结果。 在FBRequestFacebook类使用Graph API请求(不用于公共使用) FBRequest类。 这个类基本上是一个花哨的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; } 

这里的所有都是它的。 上面的方法将取消最近的请求,你永远不会再听到它。

我遵循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]; } 

2012年4月22日更新

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

这是我实现的代码:

 // 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; 

对于我们这些build立静态库并且无法访问实现文件的人来说,一个类别是最好的select。

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

这里说的是类别。

 // 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方法的第四行修复了这个问题。

试试这个,而不是使用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]; } 

由于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]; 

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

当我导航到另一个视图时,我在2012年8月有效的以前的iOS Facebook SDK发生崩溃。 我的解决scheme基于@staticfiction响应:

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

 //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]; } } 

对此URL进行CURL调用

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