Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

objective c - Force asynchrounous Firebase query to execute synchronously?

I'm designing an app that uses firebase to store user information. Below, I'm trying to write a method that queries the database, obtains the stored password, and checks it against the inputted password, returning a boolean based on whether or not they match.

-(BOOL) ValidateUser: (NSString*)username :(NSString*)password {


//initialize blockmutable true-false flag
__block bool loginFlag = true;

//initialize blockmutable password holder
__block NSString* passholder;

//check if user exists in database
//get ref to firebase
Firebase * ref = [[Firebase alloc] initWithUrl:kFirebaseURL];

//delve into users
Firebase * usersref = [ref childByAppendingPath:@"users"];

//search based on username
FQuery * queryRef = [[usersref queryOrderedByKey] queryEqualToValue: username];

//EXECUTION SKIPS THIS PORTION OF CODE
//get snapshot of things found
[queryRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *querySnapshot){
    NSLog(@"%@", querySnapshot);

    if no match found
    if (querySnapshot.childrenCount == 0)
    {
        return false
        loginFlag = false;
    }

    //otherwise store password of thing found
    else
    {
        passholder = querySnapshot.value[@"hashed_password"];
        NSLog(@"%@", passholder);
    }

}];

//CODE SKIPS TO HERE

NSLog(@"%@", passholder);

//check inputted password against database password
if (![password isEqualToString:passholder])
{
    //if they don't match, return false
    loginFlag = false;
}

return loginFlag;
}

The problem, however, is that the method terminates and returns true before the firebase query runs. Essentially, the method executes, checks placeholder values against the inputted password, returns the value for the bool, AND ONLY THEN retrieves the password(within the block). I'm not sure how to force the block to run synchronously in order to actually return the correct boolean.

Is there any way to alter the flow of the method in order to actually have it return the correct boolean value? I'm at a loss.

Thanks so much

PS: I'm aware that Firebase supports a dedicated login functionailty (AuthUser and all that); however, this project is more of a proof of concept and so we're utilizing simple, unencrypted passwords stored in the main firebase.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...