Search code examples
iossqlfacebookfacebook-graph-apifacebook-ios-sdk

SQL Query to fetch user's Facebook friend list based upon gender


I am trying to fetch the friends of the user (who is logged in into his FaceBook account) based upon the gender of his friends and based upon the pic_big!=NULL (Every friend should have a big profile picture).

Below is my code that I am writing.

NSString *queryString =  [NSString stringWithFormat:@"%@%@%@%@%@%@%i%@%@",
                          @"SELECT name, pic_big, sex, uid FROM user ", 
                          @"WHERE sex=", @"male", @" AND ",
                          @"pic_big != 'null' AND uid IN (SELECT uid2 ",
                          @"FROM friend WHERE uid  ", loggedInID, @" )",
                          @"ORDER BY name"];  
NSMutableDictionary *dictionary=[NSMutableDictionary 
    dictionaryWithObjectsAndKeys:queryString,@"query", nil];

[FBRequest getRequestWithParams:dictionary
                     httpMethod:@"GET" 
                       delegate:self
                     requestURL:@"fql.query"]; 

loggedINiD is the user's login ID. I am not getting any response in the delegate methods. I am sure I need to recheck this SQL query. Any suggestions please.


Solution

  • You have a couple of errors in your FQL code and adding one suggestion I would make for simplicity, here is what you should query instead:

    SELECT name, pic_big, sex, uid FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex = "male"

    Things to note:

    1. uid1 is the correct index field for the friend table, not uid
    2. You were missing the equals sign after "FROM friend WHERE uid"
    3. me() is a helper function in FQL which represents the current session user (ie. the app user)
    4. pic_big will never be null, as even the default Profile pic for users on Facebook returns a value for this, so testing for it is redundant.
    5. I've changed the order of the WHERE clause, but that shouldn't matter.