2014-12-07 73 views
0

我正在設計一個使用firebase來存儲用戶信息的應用程序。下面,我試圖編寫一個方法來查詢數據庫,獲取存儲的密碼,並根據輸入的密碼進行檢查,並根據是否匹配返回布爾值。強制異步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。本質上,該方法執行,根據輸入的密碼檢查佔位符值,返回bool的值,並且僅僅檢索密碼(在塊內)。我不知道如何強制該塊同步運行,以便實際返回正確的布爾值。

是否有任何方法來改變方法的流程,以實際使它返回正確的布爾值?我很茫然。

非常感謝

PS:我知道,火力地堡支持專用的登錄functionailty(爲AuthUser和所有的);然而,這個項目更多的是一個概念證明,所以我們利用存儲在主要Firebase中的簡單的,未加密的密碼。

回答