Facebook API - How to cancel Graph Request

后端 未结 9 1043
既然无缘
既然无缘 2020-12-16 04:06

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 th

9条回答
  •  Happy的楠姐
    2020-12-16 04:52

    Since SDK 3.1, it's very easy, as startWithCompletionHandler: returns a FBRequestConnection object, which has a -(void)cancel; method.

    For example:

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

提交回复
热议问题