0

I just started using Parse and have a few problems sometimes. I have a PFQueryTableView which works perfect when a user is logged in. The queryForTable() function queries all the objects that have the current users username in their "createdBy" column. But if no User is logged in, what do I return then? At the moment I have it like this, which works, but gives an error in the Log. After the user logged in, you have to pull to refresh the Table, then it shows the users events. I would like to have an empty table when no User is logged in, and after a User logged in and the loginView disappeared, the table should automatically load the users data. Here are my viewDidAppear and my queryForTable functions:

override func viewDidAppear(animated: Bool) {

    if PFUser.currentUser() == nil {
        let loginView = PFLogInViewController()
        loginView.delegate = self
        self.presentViewController(loginView, animated: true, completion: nil)



    }

}


override func queryForTable() -> PFQuery {

    if PFUser.currentUser() != nil{

            let query = PFQuery(className: "calendarEvents")
            query.cachePolicy = .CacheElseNetwork
            query.orderByAscending("StartDate")
            query.whereKey("createdBy", equalTo: (PFUser.currentUser()?.username)!)
            return query
   }else{
        let noQuery = PFQuery()

        return noQuery
    }


}
Dominic K
  • 6,975
  • 11
  • 53
  • 62
beginner_T
  • 417
  • 1
  • 6
  • 21

1 Answers1

0

The approach you've chosen is fine. A more common approach is to not even present this VC until the app has a user. You could also add all users to some role, and then protect your "calendarEvents" class with a CLP that refers to that role.

Another idea on the query is to add a qualifier that guarantees an empty result, for example...

override func queryForTable() -> PFQuery {
    let query = PFQuery(className: "calendarEvents")
    query.cachePolicy = .CacheElseNetwork
    query.orderByAscending("StartDate")
    query.whereKey("createdBy", equalTo: (PFUser.currentUser()?.username)!)

    if PFUser.currentUser() == nil {
        query.whereKey("createdAt", greaterThan: NSDate.distantFuture())  // return objects created after the universe has collapsed
    }
    return query
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • thanks for your answer! Since I just started programming and learning it all by myself, I didn't get all of your answer :D I want to be this VC to be like the entry point to my app, so how is it possible to not show it in case no user is logged in, but show it when a user is logged in? And to be honest I don't know what the whole last sentence means :D sorry, what is a CLP and how can I protect a role with it? You don't have to answer all of that, it's also fine if you maybe know some websites where I can learn about that. – beginner_T Dec 07 '15 at 06:00
  • Gotcha. Sorry. In the first idea, the vc in your question *would not* be the entry point. Instead, the initial vc would be very simple: Its UI would just say "Hello", and all it would do is check for a user and either present a login vc or your question vc. The second idea is more security related (you should probably look at the parse docs on that (https://parse.com/docs/ios/guide#security). The idea is that you can setup security that allows only users that you grant rights to to see any object or set of objects (this is extra important for web apps whose keys are out in the open) – danh Dec 07 '15 at 06:09
  • Alright, thanks a lot!! I will check the docs for any further information and add a comment if I have any more questions. Thanks again for you help! – beginner_T Dec 07 '15 at 06:12
  • I should also underscore this point: the empty query you return is good enough to get started. – danh Dec 07 '15 at 06:13
  • Okay, means a lot to me that you like this solution I made up myself. Even though it kinda annoys me that it gives me an error, although the application still runs perfectly. And it's kinda bad that the user has to pull to refresh the tableView to get to see his data. Is there a way that after the login succeeded and the loginView disappeared, the PFQueryTableView automatically reloads? I hope you get what I mean :D – beginner_T Dec 07 '15 at 06:27
  • Didn't realize you were seeing an error. Try returning nil. [self loadObjects] is how you refresh programmatically. – danh Dec 07 '15 at 06:33
  • Perfect!! loadObjects did the job ;) I already tried returning nil, it won't let me. It says nil is incompatible with return type PFQuery. At the moment I get this error: [Error]: bad characters in classname: (null) (Code: 103, Version: 1.8.5) .... But everything works perfect and how I wanted it, so can I just ignore this error? :D – beginner_T Dec 07 '15 at 06:39
  • Hmm how about the let query = ... statement you use when there is a user, but set its limit = 0?? – danh Dec 07 '15 at 06:43
  • Unfortunately it does not work, I thought of maybe creating an entry in my Parse Class, like a default entry that is loaded when no user is logged in. Do you think thats a good idea to handle that? – beginner_T Dec 07 '15 at 14:21
  • How about qualifying the query so it is certain to return an empty result? (edited to illustrate... you might need to tweak syntax, as my Swift is pretty rough). – danh Dec 07 '15 at 15:11