2012-04-25 46 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