1

有没有办法在一个 Facebook Graph API 调用中检索或删除多个 Facebook request_id?

例如,如果用户收到来自不同人对同一应用程序的多个请求,它们将被分组为一个通知,并且当用户接受通知时,所有 request_id 将作为逗号分隔列表传递给应用程序。有什么方法可以避免遍历每一个并单独检索/删除它?

4

2 回答 2

2

如果我理解正确,您可以使用批处理请求在一次调用中执行多项操作。

例如:

NSString *req01 = @"{ \"method\": \"GET\", \"relative_url\": \"me\" }";
NSString *req02 = @"{ \"method\": \"GET\", \"relative_url\": \"me/friends?limit=50\" }";
NSString *allRequests = [NSString stringWithFormat:@"[ %@, %@ ]", req01, req02];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:allRequests forKey:@"batch"];
[facebook requestWithGraphPath:@"me" andParams:params andHttpMethod:@"POST" andDelegate:self];

这仍然意味着您必须遍历通知,但您可以使用一/两个请求来执行所有操作。

于 2012-05-17T23:11:02.630 回答
2

Binyamin 是正确的,批处理请求可能会起作用。但是,我发现要通过 request_ids 获取请求数据,您可以简单地将它们作为逗号分隔的列表传递,并避免执行批处理请求。

NSString *requestIds = @"123456789,987654321";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:requestIds forKey:@"ids"];
[facebook requestWithGraphPath:@"" andParams:params andDelegate:self];

您的最终图表 URL 应如下所示:

https://graph.facebook.com/?ids=REQUEST_ID1,REQUEST_ID2,REQUEST_ID3&access_token=ACCESS_TOKEN

对于删除操作,相信还是需要批量操作的。当您从上述调用中从 FB 获取 request_id 数据时,它将是一个 NSDictionary,其中每个 result_id 作为键。您可以查看每个键并创建一个批处理操作以将它们全部删除。

NSDictionary *requests = DATA_RETURNED_FROM_FACEBOOK;
NSArray *requestIds = [requests allKeys];
NSMutableArray *requestJsonArray = [[[NSMutableArray alloc] init] autorelease];
for (NSString *requestId in requestIds) {
    NSString *request = [NSString stringWithFormat:@"{ \"method\": \"DELETE\", \"relative_url\": \"%@\" }", requestId];
    [requestJsonArray addObject:request];
}
NSString *requestJson = [NSString stringWithFormat:@"[ %@ ]", [requestJsonArray componentsJoinedByString:@", "]];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:requestJson forKey:@"batch"];
[facebook requestWithGraphPath:@"" andParams:params andHttpMethod:@"POST" andDelegate:nil];

请注意,根据https://developers.facebook.com/docs/reference/api/batch/,批处理请求的当前限制为 50 。因此,为了完全安全,您应该检查 request_ids 的计数,如果它大于 50,您将不得不执行多个批处理请求。

于 2012-05-22T23:35:03.990 回答