0

The goal is : after a user has been logged in , the next activity should display his list of courses. There is a table of courses in Parse's cloud, but how can I retrieve the correct courses (current user's courses) ? I've thought of 2 approaches : 1. using getCurrentUser method - but what if there are more than one user in the cache ? 2. save for each course the userName that saved it , and then query for that userName - but how should I know that the currentUser method will return the correct user ?

How can I solve this problem ?

1 Answer 1

1
  1. using getCurrentUser method - but what if there are more than one user in the cache ?

There's always one currently logged in user and ParseUser.getCurrentUser() will get it. Well, unless there is no logged in user.

but how should I know that the currentUser method will return the correct user

As above. I don't get what you're confused about.

There is a table of courses in Parse's cloud, but how can I retrieve the correct courses (current user's courses)

and

save for each course the userName that saved it , and then query for that userName - but how should I know that the currentUser method will return the correct user ?

I can think of two approaches here. There may be more. Up to you what you decide to use.

So you have the _User table and the Course table. You can do one of the following:

  1. Add a courses array to each _User and for each _User, store their list of courses there. It should be a list of objectIds, so basically you'll have the ID's of all the courses that _User is registered for. Each Parse object has a limited size in the database so you should use it if you are sure you won't need big amounts of data for each user. If you want to retrieve this, query the _User table by the id of the user and fetch the list of courses.

  2. The "relational" approach: add an UserCourse table that has a userId and a courseId column. Every time you want to add a course for a particular user, you add a new entry in here that holds the id of that user and the id o the course. Similarly to the above, have a query that fetches all the stuff from UserCourse by userId. Note that in this case you'll also need to make sure your query fetches the data from _User and Course. Otherwise you'll just end up with UserCourse entries, which merely store ID's.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess I don't understand how ParseUser.getCurrentUser() works. If 2 different users login from the same device , so the cache has the data of the last one who logged in ?
It's not the last user, but the currently logged in user. So if user A logs in, logs out, and user B logs in, then user B is returned. If user B then logs out, then null is returned. And if user A now logs in, then user A is returned. And so on.
thanks, by the way , regarding to your first approach , how do I save an array as a value in ParseObject ?
@user704023 By using the ParseObject.add() method: parse.com/docs/android/api/com/parse/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.