0

I have a class which I am using to interact with Facebook in Objective C for my iPhone application.

GWFacebook.h

@interface GWFacebook : Facebook <FBRequestDelegate>
{
   bool query_running;
   NSArray* query_result;
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;
- (void)request:(FBRequest *)request didLoad:(id)result;
- (void)LogIn;
- (NSArray*)getAllFriends;

@end

GWFacebook.m

#import "GWFacebook.h"

@implementation GWFacebook
- (void)request:(FBRequest *)request didLoad:(id)result
{
 query_result = result;
 query_running = false;
 }

- (NSArray*)getAllFriends
{
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                @"SELECT first_name, last_name, uid, pic_big FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY mutual_friend_count desc",@"query",nil];   
    query_running = true;
    bool localbool = true;
    [self requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self];
   while(localbool)   
      localbool = query_running;

    return query_result;
}
@end

Basically the function getAllFriends makes a fb request and sets this object as the facebook request delegate. The function request didLoad of the delegate (self) is then called and uses a variable to pass the data back to the getAllFriends function, which then returns it to whatever called it.

The long and short of it is that im trying to use a while loop to stall the function until it receives a response. In debugging, its clear that the request didload function is never called by the facebook object. It is called when I remove the while loop ... thus the data request is successful and the while loop is the problem.

Im wondering if there is a way to stall a function until the data is received...Im probably doing something very silly so I need some help!

Thanks

BYZZav
  • 1,418
  • 1
  • 19
  • 35

2 Answers2

2

The Facebook request will perform the delegate callback on your thread, which you block in the while loop. That is what is causing your problem.

As a matter of design, you have an API which works rather asyncronously, but you created a syncronous wrapper over it. Do you need to do it that way? I would recommend that you change your GWFacebook class to use a delegate or a block for returning the NSArray of friends.

- (void)getAllFriends:(void (^)(NSArray *))callbackBlock

This way you do not block your UI and you may find it easier to provide any kind of waiting indicators in your UI.

David V
  • 11,531
  • 5
  • 42
  • 66
1

You should use NSNotificationCenter for loading things like that.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

Send and receive messages through NSNotificationCenter in Objective-C?

Make your own custom selector for when it's done.

Community
  • 1
  • 1
TigerCoding
  • 8,710
  • 10
  • 47
  • 72