title: How to show log in screen before the root view controller (a PFQueryTableViewController) attempts a query without a user and crashes the program
body: <p>I want my root view controller of the program that I am working on to be a <code>PFQueryTableViewController</code>, however, I need the app to make sure that a user is logged in before the <code>PFQueryTableViewController</code> attempts a query. </p>
  
  <p>I tried creating a <code>RootTabBarViewController</code> whose initial view is the <code>PFQueryTableViewController</code> and putting the following in the <code>RootTabBarViewController</code>'s .h and .m files:</p>
  
  <pre><code>//
  //  RootTabBarViewController.h
  //
  
  #import &lt;UIKit/UIKit.h&gt;
  #import &lt;Parse/Parse.h&gt;
  
  @interface RootTabBarViewController : UITabBarController &lt;PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate&gt;
  
  @end
  
  
  
  //
  //  RootTabBarViewController.m
  //
  
  #import "RootTabBarViewController.h"
  #import "PlannrLogInViewController.h"
  
  @interface RootTabBarViewController ()
  
  @end
  
  @implementation RootTabBarViewController
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (void)viewDidAppear:(BOOL)animated
  {
      [super viewDidAppear:animated];
  
      if (![PFUser currentUser]) { // No user logged in
          // Create the log in view controller
          PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
          [logInViewController setDelegate:self]; // Set ourselves as the delegate
  
          // Create the sign up view controller
          PFSignUpViewController *signUpViewController = [[PFSignUpViewController 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];
      }
  }
  /*
  #pragma mark - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
  }
  */
  
  #pragma mark - PFLoginViewController Delegate
  
  // Sent to the delegate to determine whether the log in request should be submitted to the server.
  - (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
      // Check if both fields are completed
      if (username &amp;&amp; password &amp;&amp; username.length != 0 &amp;&amp; password.length != 0) {
          return YES; // Begin login process
      }
  
      [[[UIAlertView alloc] initWithTitle:@"Missing Information"
                                  message:@"Make sure you fill out all of the information!"
                                 delegate:nil
                        cancelButtonTitle:@"ok"
                        otherButtonTitles:nil] show];
      return NO; // Interrupt login process
  }
  
  // Sent to the delegate when a PFUser is logged in.
  - (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
      [self dismissViewControllerAnimated:YES completion:NULL];
  }
  
  // Sent to the delegate when the log in attempt fails.
  - (void)logInViewController:(PFLogInViewController *)logInController didFailToLogInWithError:(NSError *)error {
      NSLog(@"Failed to log in...");
  }
  
  // Sent to the delegate when the log in screen is dismissed.
  - (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController {
      [self.navigationController popViewControllerAnimated:YES];
  }
  
  #pragma mark - PFSignUpViewController Delegate
  
  // Sent to the delegate to determine whether the sign up request should be submitted to the server.
  - (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info {
      BOOL informationComplete = YES;
  
      // loop through all of the submitted data
      for (id key in info) {
          NSString *field = [info objectForKey:key];
          if (!field || field.length == 0) { // check completion
              informationComplete = NO;
              break;
          }
      }
  
      // Display an alert if a field wasn't completed
      if (!informationComplete) {
          [[[UIAlertView alloc] initWithTitle:@"Missing Information"
                                      message:@"Make sure you fill out all of the information!"
                                     delegate:nil
                            cancelButtonTitle:@"ok"
                            otherButtonTitles:nil] show];
      }
  
      return informationComplete;
  }
  
  // Sent to the delegate when a PFUser is signed up.
  - (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user {
      [self dismissModalViewControllerAnimated:YES]; // Dismiss the PFSignUpViewController
  }
  
  // Sent to the delegate when the sign up attempt fails.
  - (void)signUpViewController:(PFSignUpViewController *)signUpController didFailToSignUpWithError:(NSError *)error {
      NSLog(@"Failed to sign up...");
  }
  
  // Sent to the delegate when the sign up screen is dismissed.
  - (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController {
      NSLog(@"User dismissed the signUpViewController");
  }
  
  @end
  </code></pre>
  
  <p>And here is my .m file for the PFQueryTableViewController subclass:</p>
  
  <pre><code>//
  //  EventPFQueryTableViewController.m
  //
  
  #import "EventPFQueryTableViewController.h"
  #import "Event.h"
  
  @interface EventPFQueryTableViewController ()
  
  @end
  
  @implementation EventPFQueryTableViewController
  
  
  - (id)initWithCoder:(NSCoder *)aDecoder{
  
      self = [super initWithCoder:aDecoder];
      if (self)
      {
  
          self.parseClassName = kEventListClassKey;
      }
  
      return self;
  }
  
  - (PFQuery *)queryForTable
  {
      PFQuery *query = [Event query];
      [query whereKey:kEventListFieldKeyUser equalTo:[PFUser currentUser]];
  
      return query;
  }
  /*
  
  @end
  </code></pre>
  
  <p>However, the code in the <code>viewDidLoad</code> function of this tab bar view controller does not seem to run before the <code>PFQueryTableViewController</code> (the first view of the root bar view controller) is loaded and attempts to query because I still get the exception: </p>
  
  <blockquote>
    <p>2014-07-10 20:46:04.226 Plannr[12810:60b] <em>*</em> Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)'</p>
  </blockquote>
  
  <p>How would I make sure that the log in view loads far before the <code>EventPFQueryTableViewController</code>? Thanks in advance.</p>