在iOS SDK中发出请求,循环过快

时间:2012-04-25 22:16:50

标签: iphone ios xcode facebook-graph-api

我有一个朋友ID数组,我想调用它然后存储返回的数据对象。

看起来像这样:

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

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

问题是我想我发出的请求太频繁了,而且没有让FB有机会正确地返回数据?我怎样才能解决这个问题?感谢

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)