概述:我正在使用Amazon DynamoDB作爲我的登錄服務。我的登錄方法在UserAccount.m中看起來像這樣。我在LoginViewController.m上調用這個類的方法登錄按鈕tap:目標c-如何在執行任何其他代碼之前確保塊執行得更進一步?
+ (BOOL)loginWithUsername:(NSString *)username password:(NSString *)password{
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
BOOL __block isLoginSuccessful = NO;
[[dynamoDBObjectMapper load:[User class] hashKey:username rangeKey:@"key"]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
//Do something with the result.
User *user = task.result; // User is a model I'm using
NSLog(@"pass: %@", user.password);
// Check returned password from DynamoDB with user-supplied password.
if ([user.password isEqualToString:password]) {
isLoginSuccessful = YES;
}
}
return nil;
}];
return isLoginSuccessful; // <-- Issue: function returns before block executes and isLoginSuccessful value is changed.
}
問題是函數返回之前執行塊。我的嘗試:
我)我讀了有關使用dispatch groups on this SO question
II)嘗試在這樣的主線程中執行的方法- (AWSTask *)load:(Class)resultClass hashKey:(id)hashKey rangeKey:(id)rangeKey
但仍函數返回前塊執行完畢:
dispatch_async(dispatch_get_main_queue(), ^{
[[dynamoDBObjectMapper load:[User class]
hashKey:username
rangeKey:@"key"] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
if (!task.error) {
User *user = task.result;
NSLog(@"pass: %@", user.password);
//Do something with the result.
if ([user.password isEqualToString:password]) {
isLoginSuccessful = YES;
}
} else {
NSLog(@"Error: [%@]", task.error);
}
return nil;
}];
});
上午我錯過了某些東西/在我的方法中做錯了什麼?任何對正確方向的建議都會很有幫助。謝謝!
編輯1:包含從那裏
loginWithUsername
類的方法被調用函數:
@IBAction func login(sender: AnyObject) {
if(UserAccount.loginWithUsername(txtEmail.text, password: txtPassword.text)){
println("SUCCESS")
lblIncorrect.hidden = true
}
else{
println("Incorrect username/password")
lblIncorrect.hidden = false
}
}
嘗試使用完畢塊 – Rajal