【问题标题】:Force asynchrounous Firebase query to execute synchronously?强制异步 Firebase 查询同步执行?
【发布时间】:2014-12-07 01:27:44
【问题描述】:

我正在设计一个使用 firebase 存储用户信息的应用。下面,我正在尝试编写一个方法来查询数据库,获取存储的密码,并根据输入的密码检查它,根据它们是否匹配返回一个布尔值。

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

然而,问题在于该方法在 firebase 查询运行之前终止并返回 true。本质上,该方法执行,根据输入的密码检查占位符值,返回布尔值,然后才检索密码(在块内)。我不确定如何强制块同步运行以实际返回正确的布尔值。

有没有什么方法可以改变方法的流程以使其真正返回正确的布尔值?我不知所措。

非常感谢

PS: 我知道 Firebase 支持专用的登录功能(AuthUser 等);然而,这个项目更多的是概念验证,因此我们使用了存储在主 firebase 中的简单、未加密的密码。

【问题讨论】:

    标签: objective-c asynchronous firebase objective-c-blocks


    【解决方案1】:

    要强制您的函数等待某个异步操作完成,请查看信号量。这是另一个解决它的 StackOverflow 问题:objective c - How do I wait for an asynchronously dispatched block to finish?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多