iphone - 在 iOS SDK 中发出请求,循环飞得太快

标签 iphone ios xcode facebook-graph-api

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

看起来像这样:

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

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

问题是我想我发送请求的频率太高了,没有给 FB 机会正确返回数据?我怎样才能解决这个问题?谢谢

最佳答案

这是您需要做的。这非常类似于您使用 NSURLConnection API 发出异步请求的方式。

在你的 header/.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];
}

关于iphone - 在 iOS SDK 中发出请求,循环飞得太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324610/

相关文章:

javascript - 如何在 iPhone 上的 DIV 上实现 100% [window] 高度?

ios - 删除 subview 然后将其添加回来

ios - 选择表格 View 单元格时显示自定义 UIImageView

iphone - 无法实例化名为 UICollectionView 的类

ios - 使用 Kii 查询结果

objective-c - 修饰符标志 : NSInteger enough?

iphone - 如何在 iPhone 或 iPad 中获取进程 ID?

iphone - 我想跟踪手指移动的速度(以像素每秒为单位)有可能吗?

iphone - segue.destinationViewController 究竟做了什么?

ios - 设置 UICollectionViewLayout 以匹配像结果一样的列拆分 UITableView