Using a while loop to stall until I get some data back

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

Answers


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.


Need Your Help

Grid line consistent with ticks on axis

r graph gridlines

I am embarrassed to ask this simple question, but has been in kicking my mind for several days whenever I create a plot:

PHP fileupload: keep both files if same name

php file-upload file-rename

is there any pretty solution in PHP which allows me to expand filename with an auto-increment number if the filename already exists? I dont want to rename the uploaded files in some unreadable stuf...