Reputation: 1650
Is there any way to retrieve or delete multiple Facebook request_ids in one Facebook Graph API call?
For example, if a user receives multiple requests from different people for the same app, they will be grouped as one notification and all the request_ids will be passed to the app as a comma separated list when the user accepts the notification. Is there any way to avoid having to loop through each one and individually retrieve/delete it?
Upvotes: 1
Views: 585
Reputation: 1650
Binyamin is correct that batch requests would probably work. However, I discovered that to fetch request data by request_ids, you can simply pass them as a comma separate list and avoid doing a batch request.
NSString *requestIds = @"123456789,987654321";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:requestIds forKey:@"ids"];
[facebook requestWithGraphPath:@"" andParams:params andDelegate:self];
Your final graph URL wants to look like:
https://graph.facebook.com/?ids=REQUEST_ID1,REQUEST_ID2,REQUEST_ID3&access_token=ACCESS_TOKEN
For the delete operation, I believe a batch operation is still required. When you get the request_id data back from FB from the above call, it will be an NSDictionary with each result_id as a key. You can look at each key and create a batch operation to delete them all.
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];
Note that the current limit on batch requests is 50, per https://developers.facebook.com/docs/reference/api/batch/. So to be completely safe you should check the count of request_ids and if it is greater than 50, you will have to do multiple batch requests.
Upvotes: 2
Reputation: 137322
If I understand you correctly, you can use batch request to perform multiple operations in one call.
For example:
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];
It still means you have to iterate over the notifications, but you can use one/two requests to perform all actions.
Upvotes: 2