簡體   English   中英

在多個Facebook request_ids上執行操作

[英]Performing operation on multiple Facebook request_ids

有沒有辦法在一個Facebook Graph API調用中檢索或刪除多個Facebook request_ids?

例如,如果用戶收到來自同一應用程序的不同人的多個請求,則它們將被分組為一個通知,並且當用戶接受通知時,所有request_id將作為逗號分隔列表傳遞給應用程序。 有沒有辦法避免必須遍歷每個並單獨檢索/刪除它?

如果我理解正確,您可以使用批處理請求在一次調用中執行多個操作。

例如:

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

它仍然意味着您必須迭代通知,但您可以使用一個/兩個請求來執行所有操作。

Binyamin是正確的,批量請求可能會起作用。 但是,我發現要通過request_ids獲取請求數據,您只需將它們作為逗號分隔列表傳遞,並避免執行批處理請求。

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

您的最終圖表網址希望如下所示:

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

請注意,批量請求的當前限制為50,按照https://developers.facebook.com/docs/reference/api/batch/ 所以為了完全安全,你應該檢查request_ids的數量,如果它大於50,你將不得不做多個批量請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM