Reputation: 588
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 ?
Upvotes: 0
Views: 1034
Reputation: 1537
- 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:
Add a courses
array to each _User
and for each _User
, store their list of courses there. It should be a list of objectId
s, 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.
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.
Upvotes: 1