user2427421
user2427421

Reputation: 3

Populate UITableView with Facebook friends

I'm trying to populate a UITabelView with Facebook friend's list. I had checked all the related questions and try all the answers, however, it is still not working. It's been more than 4 hours that this simple code is wasting my time.

@interface InviteViewController ()

{

__block NSArray *friends;
__block NSMutableArray *mArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends",friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
        [mArray addObject:friend.name];
    }

}];
NSLog(@"%d",mArray.count);
return mArray.count;
}

and here is my console log:

2014-01-12 20:18:21.429 PixidizeStoryBoard[26269:60b] 0
2014-01-12 20:18:21.742 PixidizeStoryBoard[26269:60b] Found: 1 friends
2014-01-12 20:18:21.744 PixidizeStoryBoard[26269:60b] I have a friend named Siavash ALp with id 1524650444

Upvotes: 0

Views: 627

Answers (2)

user2427421
user2427421

Reputation: 3

- (void)viewDidLoad
{
[super viewDidLoad];
//[self.spinnerView setHidden:YES];
self.navigationController.title = @"Create Event";
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    __block NSArray *friends;

    friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends",friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);

    }
    [self setArrayWithArray:friends];
}];

// Do any additional setup after loading the view.
}
- (void)setArrayWithArray:(NSArray *)array
{
self.myFriends = [NSMutableArray array];
for (NSDictionary<FBGraphUser>* friend in array) {
    [self.myFriends addObject:friend.name];

}
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"invite"]) {
    InviteViewController *ivc = segue.destinationViewController;
    ivc.myImage = self.img;
    ivc.nameField2 = self.nameField.text;
    ivc.locationField2 = self.locationField.text;
    ivc.friendsArray = self.myFriends;
}
}

Upvotes: 0

ansible
ansible

Reputation: 3579

It looks like it is working. It's finding one friend.

I think the call to startWithCompletionHandler: is an async call. So it will call the tag, then immediately run the line NSLog(@"%d",mArray.count); and return a still empty NSMutableArray. Then after the this block is called from a background thread.

^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends",friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
        [mArray addObject:friend.name];
    }

And you can see one friend is being logged to NSLog, but that is why it happens after your first call to NSLog. So instead of trying to return an NSMutableArray back, add something to the end of your block that updates your UITableView instead.

Does that make sense?

Upvotes: 1

Related Questions