2011-03-18 118 views
4

我正在開發一個帶有FB連接的iPhone iOS應用程序。Facebook連接批量請求和FQL錯誤問題

我想爲我的每個朋友獲取大量數據,並且需要多個請求。

我不知道在iOS SDK中是否有使用批量請求的方法?

和FQL多查詢的其他問題。以下查詢僅適用於一個朋友的限制!奇怪的。

 
SELECT eid FROM event_member WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 2)

則返回錯誤The operation couldn’t be completed. (facebookErrDomain error 1.) 根據本example of facebook它應該一直在努力。

回答

14

您可以使用Facebook iOS SDK進行批量請求,但您需要自己準備JSON請求。下面是一個例子:

  • 首先,創建含有2個JSON請求(見http://developers.facebook.com/docs/api/batch)JSON數組字符串。您可以使用您的首選JSON API來創建這些字符串。
  • 其次,使用映射到JSON請求字符串的「批處理」鍵創建參數字典。
  • 然後發送請求。請注意,您需要將某些東西放在requestWithGraphPath中。我只是把「我」(這個請求不考慮)。你也必須以POST方法發送它。
  • 最後,在request:didLoad中等待響應數組。

-(void) prepareMyBatchRequest { 
    NSString *jsonRequest1 = @"{ \"method\": \"GET\", \"relative_url\": \"me/friends\" }"; 
    NSString *jsonRequest2 = @"{ \"method\": \"GET\", \"relative_url\": \"me/albums\" }"; 
    NSString *jsonRequestsArray = [NSString stringWithFormat:@"[ %@, %@ ]", jsonRequest1, jsonRequest2]; 
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:jsonRequestsArray forKey:@"batch"]; 
    [facebook requestWithGraphPath:@"me" andParams:params andHttpMethod:@"POST" andDelegate:self]; 
} 

- (void)request:(FBRequest *)request didLoad:(id)result { 
    NSArray *allResponses = result; 
    for (int i=0; i < [allResponses count]; i++) { 
     NSDictionary *response = [allResponses objectAtIndex:i]; 
     int httpCode = [[response objectForKey:@"code"] intValue]; 
     NSString *jsonResponse = [response objectForKey:@"body"]; 
     if (httpCode != 200) { 
      NSLog(@"Facebook request error: code: %d message: %@", httpCode, jsonResponse); 
     } else { 
      NSLog(@"Facebook response: %@", jsonResponse); 
     } 
    } 
} 

關於您的其他問題,我不知道(試着問在不同崗位的其他問題,因此更容易跟進)。

+0

謝謝!你知道爲什麼圖API請求參數像極限,並且因爲有問題嗎?它使用的是「events?limit ..」而不是「events&limit ..」,它不會獲得更多的參數。至於'since =今天'它會帶來甚至老的事件! Tnx – 2011-04-01 07:06:45

+0

這裏的facebook對象是什麼 - [facebook requestWithGraphPath:@「me」andParams:params andHttpMethod:@「POST」andDelegate:self];它是FBSession還是FBRequest? – verma 2012-10-07 22:16:47

+0

@verma我想知道同樣的問題 – Ricardo 2015-02-16 15:21:18