每次导航都会执行查询

时间:2014-09-11 22:06:43

标签: objective-c parse-platform

我在主菜单的ViewDidAppear中有以下解析查询,它查询管理员是否通过Parse登录。问题是每次用户导航到菜单时都会执行查询,因为它是主屏幕;这很常见。这是查询,我把它放在if语句中,基本上说一旦登录,停止查询或者应该将其粘贴到另一个方法中:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

PFACL *roleACL = [PFACL ACL];
[roleACL setPublicReadAccess:YES];
PFRole *role = [PFRole roleWithName:@"Administrator" acl:roleACL];
[role saveInBackground];

if (![PFUser currentUser]) {
    // Create the log in view controller
    CustomLoginViewController *logInViewController = [[CustomLoginViewController alloc] init];
    [logInViewController setDelegate:self]; // Set ourselves as the delegate.

    // Create the sign up view controller
    CustomSignupViewController *signUpViewController = [[CustomSignupViewController alloc] init];
    [signUpViewController setDelegate:self]; // Set ourselves as the delegate

    // Assign our sign up controller to be displayed from the login controller
    [logInViewController setSignUpController:signUpViewController];

    // Present the log in view controller
    [self presentViewController:logInViewController animated:YES completion:NULL];
} else {
    PFQuery *queryRole = [PFRole query];
    [queryRole whereKey:@"name" equalTo:@"Administrator"];
    [queryRole getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        PFRole *role = (PFRole *)object;
        PFRelation *adminRelation = [role users];
        PFQuery *queryAdmins = [adminRelation query];
        [queryAdmins whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
        [queryAdmins getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
            if (!error && [object.objectId isEqualToString:[PFUser currentUser].objectId]) {
                SelectUserViewController * GPVC = [[SelectUserViewController alloc] initWithNibName:@"SelectUserViewController" bundle:nil];
                [self presentViewController:GPVC animated:YES completion:nil];
            }

        }];
    }];
}

}

我还在shouldBeginSignUp部分中进一步查询了另一个问题。这真让我烦恼,我的导师也没有提交我的应用程序。用现在的查询。

2 个答案:

答案 0 :(得分:0)

基本上只是感谢CrimsonChris,将以下代码放在viewDidLoad中意味着查询只会在用户第一次进入主菜单时执行,而不是每次都进行操作。解决了问题。谢谢Chris,

答案 1 :(得分:0)

您可能想要考虑将此代码放入一个处理您的Web服务的单独模型对象中,特别是如果其他视图控制器将进行Web调用...它具有更清晰(并且在MVC的精神)中具有相关性在适当的模型中将非UI功能组合在一起!