力 - 异步火力地堡查询同步执行? [英] Force asynchrounous Firebase query to execute synchronously?

查看:200
本文介绍了力 - 异步火力地堡查询同步执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计使用火力来存储用户信息的应用程序。下面,我想编写查询数据库的方法,取得存储的密码,并检查其对所输入的密码,根据他们是否匹配返回一个布尔值。

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;
}

但问题是,该方法终止,火力查询运行之前返回true。本质上,该方法执行,检查,对占位符输入的密码值,返回的布尔值,然后才取回密码(块内)。我不知道如何强制块,以实际返回正确的布尔同步运行。

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.

非常感谢

PS:我知道,火力地堡支持专用登录functionailty(为AuthUser和所有的);然而,该项目更是一个概念证明的,因此我们就可以利用存储在主火力点简单的,未加密的口令。

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.

推荐答案

要强迫你的函数等待某些异步操作完成,检查出的信号灯的。这里的另一个问题计算器其中铲球它:<一href=\"http://stackoverflow.com/questions/4326350/how-do-i-wait-for-an-asynchronously-dispatched-block-to-finish\">objective Ç - 如何等待一个异步分派块完成

To force your function to wait for some asynchronous action to complete, check out semaphores. Here's another StackOverflow question which tackles it: objective c - How do I wait for an asynchronously dispatched block to finish?

这篇关于力 - 异步火力地堡查询同步执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆