-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a better way for relational queries ? #1741
Comments
🐬 |
🐳 |
Hi @github2016a You can use Also I notice that you are using a foreignKey approach to Realm, that is not good for NoSQL type dbs! A better way is to just store the objects onto eachother, similar to how you stored an array of books on Person. class Book: RLMObject {
dynamic var ownerId : String = ""
dynamic var title : String = ""
dynamic var color : String = ""
} Should be class Book: RLMObject {
dynamic var owner: Person?
dynamic var title : String = ""
dynamic var color : String = ""
} Objects are related to other objects because they are stored within each other. You shouldn't need to use any id's to reference them. Id's are good for primary keys if you need to find a very specific objects to update them for example. For the most part you should be fine without them though! It might look a little weird to you at first since you will be duplicating data in some places, but that's fine, that is the power of NoSQL. It makes your queries much cleaner, and the ability to grab the objects you need much more straightforward |
thanks @yoshyosh ,that helps a lot.wish you a good day.:sunny: |
No problem! Don't hesitate to ask if you run into any other problems, I know how it can be transitioning away from SQL type thinking. Enjoy your weekend! |
Hey @yoshyosh, you've been really helpful for me in the past and I have a similar question. I have no experience with NoSQL-type db's so I was hoping you could help me figure out my schema for my weight-training tracking app. Here's what I've got: class Exercise : RLMObject // Squats, bench press, etc.
{
dynamic var name = "none"
dynamic var defaultReps = 0
dynamic var defaultSets = 0
}
class Workout : RLMObject // Contains multiple exercises
{
dynamic var name: String = ""
dynamic var isActive = false
dynamic var lastDateCompleted = NSDate(timeIntervalSince1970: 1)
dynamic var exerciseList = RLMArray(objectClassName: Exercise.className())
}
class SwiftIntObject : RLMObject // Since I couldn't figure out how to make a RLMArray of just type Int
{
dynamic var weightLifted = 0
}
class CompletedWorkout : RLMObject
{
dynamic var workout = Workout() // Contains all the exercises
dynamic var dateCompleted = NSDate(timeIntervalSinceNow: 0)
// Everything below is where I get confused:
dynamic var weightLiftedForExercise = RLMArray(objectClassName: SwiftIntObject.className())
dynamic var totalCompletedSetsForExercise = RLMArray(objectClassName: SwiftIntObject.className())
dynamic var totalCompletedRepsForExercise = RLMArray(objectClassName: SwiftIntObject.className())
} For the Second question: Exercises that have the same names like "Squat" can have a different number of reps and sets from each other, e.g. some workout routines have 3 sets of 5 reps and some have 5 sets of 5 reps. Since each Exercise will need to be unique in that regard, should I add a primary key to it? I might be overthinking all of this... |
Hey @mgallagher I think the best way to do this would be to set up your model like this Plans would be your general templates, when a user selects a plan, you can create a workout by copying its values. Workout has a completed boolean so that you know whether a user has completed it or not. You can also have some computed properties like totalReps, totalSets, totalWeight, where you would iterate through all the exercises and their sets, then add all those up. Exercise has sets rather than reps and weight, this allows us to have those varying reps and weight combos for the same type of exercise, like a squat Hope this helps! |
That's fantastic and actually makes a lot sense. Thank you very much @yoshyosh! |
Hi :
Thanks for segiddins's answer for #1736 ,sorry to bother again. I had saw the doc several times,but still cannot find a better way.sorry for my poor english,I hope i can explain clearly.
for example,there are two objects:
If i want to see the person with age>40 who has red books(show all book title)
In relational database i just need:
And in Realm I have to:
When the table data is big,the FOR still take time.There are so many such queries(such as group by) in my project,so it bothered me a lot.
If i made any mistake,or if there is a better way,please let me know.
Best Regards.
The text was updated successfully, but these errors were encountered: