2012-04-25 62 views
1

我有一個我想調用的朋友ID數組,然後存儲返回的數據對象。在iOS SDK中發出請求,循環飛得太快

看起來是這樣的:

for (int i = 0; i < self.friendIDArray.count; i++){ 

      self.detailedRequest = [facebook requestWithGraphPath:[self.friendIDArray objectAtIndex:i] andDelegate:self]; 
     } 

問題是,我想我發出請求過於頻繁,不給FB在回正常返回數據的機會嗎?我怎樣才能解決這個問題?謝謝

回答

1

這是你需要做的。這非常接近地模仿你如何使用NSURLConnection API進行異步請求。

在你的頭/ .h文件中創建一個類的屬性(成員變量,無論你怎麼稱呼它)

//header/.h file 
int indexOfLastFriendLoaded; 

在您的實現文件:

- (void) loadFriend:(int) indexOfFriendToLoad { 
    [facebook requestWithGraphPath:[self.friendIDArray objectAtIndex:indexOfFriendToLoad] andDelegate:self]; 
} 

//this method is called when each facebook request finishes, 
- (void)request:(FBRequest *)request didLoad:(id)result { 
    //first do whatever you need to with "result" to save the loaded friend data 

    //We make the next request from this method since we know the prior has already completed. 
    if (indexOfLastFriendLoaded < [self.friendIDArray count]) { 
     [self loadFriend:indexOfLastFriendLoaded]; 
     indexOfLastFriendLoaded++; 
    } 
} 

- (void) viewDidLoad { 
    //initialize facebook object first 

    indexOfLastFriendLoaded = 0; 
    [self loadFriend:indexOfLastFriendLoaded]; 
} 
0

請求的API建議你做一個委託來處理結果。

https://developers.facebook.com/docs/reference/iossdk/FBRequestDelegate/

+0

我做的,我用的是didLoad方法,但它只在最後執行一次。我想因爲我的循環太快發出請求,搞亂了響應? – user339946 2012-04-25 23:07:16

+0

嘗試像這樣http://www.iphonedevsdk.com/forum/iphone-sdk-development/1269-sleep-equivalent-iphone.html以及只要你不在應用程序的代表 – arhimmel 2012-04-25 23:27:37