gpt4 book ai didi

ios - 为什么我的代码向后运行?或者看起来是这样?

转载 作者:行者123 更新时间:2023-11-28 22:02:29 24 4
gpt4 key购买 nike

我有一个 IBAction,当被调用时,它会向 Parse.com 注册用户信息...我只实现了电子邮件和用户名方法,因为我需要确保在提交之前服务器我添加了一个计数器 int,如果 counter == 2 它将执行注册...令我惊讶的是,当我运行代码时,它会运行倒过来,所以我在底部的条件语句甚至没有被审查,为什么会这样?

这是我的代码:

- (IBAction)signMeUpButton:(id)sender {
[self.view endEditing:YES];
counter = 0;



user = [PFUser user];

NSString *emailFromTextField = self.emailTF.text;

if ([self isValidEmailAddress:emailFromTextField]) {


[self emailHasBeenTaken:emailFromTextField completion:^(BOOL emailIsTaken, NSError *error) {
if (error) {
// TODO: handle any errors here
return;
}

if (!emailIsTaken) {
emailString = emailFromTextField;
user.email = emailString;
counter++;
NSLog(@"The email is %@ the counter is %i", emailString, counter);

}
else {
[self duplicateEmail];
}
}];


}

NSString *usernameFromTextField = self.usernameTF.text;
if (usernameFromTextField.length >= 1) {
[self usernameHasBeenTaken:usernameFromTextField completion:^(BOOL usernameIsTaken, NSError *error) {
if (error) {
return;
}

if (!usernameIsTaken) {
usernameString = usernameFromTextField;
user.username = usernameString;
counter++;
NSLog(@"The username is %@ and the counter is %i", usernameString, counter);
}
else {
//
}
}];
}

if (counter == 2) {
NSLog(@"Its working");
}
}

这就是我运行它时在控制台上看到的内容,正如您所看到的,usernameHasBeenTaken 被首先调用,即使我将其写入第二...这是为什么?

2014-07-17 23:18:12.169 app[28210:60b] in the usernameHasBeenTaken. USERNAME IS NOT EXISTENT
2014-07-17 23:18:12.170 app[28210:60b] The username is sample and the counter is 1
2014-07-17 23:18:15.328 app[28210:60b] in the emailHasBeenTaken, EMAIL IS NOT EXISTENT
2014-07-17 23:18:15.328 app[28210:60b] The email is sample@email.com the counter is 2

这是我的 2 个方法:usernameHasBeenTakenemailHasBeenTaken

- (void)emailHasBeenTaken:(NSString *)email completion:(void(^)(BOOL emailIsTaken, NSError *error))completionBlock
{
void (^completionCopy)(BOOL, NSError *) = [completionBlock copy];

PFQuery *query = [PFUser query];
[query whereKey:@"email" equalTo:email];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

if (error) {
NSLog(@"in the emailHasbeenTaken ERROR HAS OCCURRED");
if (completionCopy) {
completionCopy(NO, error);
}
return;
}

if (objects.count > 0) {
NSLog(@"in the emailHasbeenTaken EMAIL IS DUPLICATE");
if (completionCopy) {
completionCopy(YES, nil);
}
}
else {
NSLog(@"in the emailHasBeenTaken, EMAIL IS NOT EXISTENT");
if (completionCopy) {
completionCopy(NO, nil);
}
}
}];
}

- (void) usernameHasBeenTaken:(NSString *)username completion:(void(^)(BOOL usernameIsTaken, NSError *error))completionBlock
{
void (^completionCopy)(BOOL, NSError *) = [completionBlock copy];
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:username];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"in the usernameHasBeenTaken ERROR HAS OCCURED");
if (completionCopy) {
completionCopy(NO, error);
}
return;
}

if (objects.count > 0) {
NSLog(@"in the usernameHasBeenTaken USERNAME IS DUPLICATE");
if (completionCopy) {
completionCopy(NO, nil);
}
}
else {
NSLog(@"in the usernameHasBeenTaken. USERNAME IS NOT EXISTENT");
if (completionCopy) {
completionCopy(NO, nil);
}
}
}];
}

最佳答案

您对完成 block 的使用表明您正在异步运行它们。如果是这种情况,那么在您调用 emailHasBeenTaken:completion:

后,您的密码条件会立即被触发

如果您必须运行异步(如果您正在访问 Web 服务,则应该这样做),则需要将您的调用嵌套在完成 block 中。

编辑

看来是这样的。 Parse API 异步运行,以避免锁定 UI 线程。您将需要嵌套您的回调,如下所示。

- (IBAction)signMeUpButton:(id)sender {
[self.view endEditing:YES];
counter = 0;



user = [PFUser user];

NSString *emailFromTextField = self.emailTF.text;

if ([self isValidEmailAddress:emailFromTextField]) {


[self emailHasBeenTaken:emailFromTextField completion:^(BOOL emailIsTaken, NSError *error) {
if (error) {
// TODO: handle any errors here
return;
}

if (!emailIsTaken) {
emailString = emailFromTextField;
user.email = emailString;
counter++;
NSLog(@"The email is %@ the counter is %i", emailString, counter);

NSString *usernameFromTextField = self.usernameTF.text;
if (usernameFromTextField.length >= 1) {
[self usernameHasBeenTaken:usernameFromTextField completion:^(BOOL usernameIsTaken, NSError *error) {
if (error) {
return;
}

if (!usernameIsTaken) {
usernameString = usernameFromTextField;
user.username = usernameString;
counter++;
NSLog(@"The username is %@ and the counter is %i", usernameString, counter);
}
else {
//
}

if (counter == 2) {
NSLog(@"Its working");
}
}];
}
}
else {
[self duplicateEmail];
}
}];


}
}

关于ios - 为什么我的代码向后运行?或者看起来是这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com