I'm using Parse (www.parse.com) for the backend database for an iPhone app. I'm creating a fitness application and want advice as the best way to structure the classes and relationships.
A few needs for the database:
1)Sets(have attribute or weight and reps)
2)Exercise (a single instance of an Exercise which can contain multiple Sets)
3)Workout (which will be a single instance for a particular workout which will represent 1 single day. No single day can have 2 Workout objects. Can contain multiple Exercise objects.
Then I also need some classes for Routine which is independent from the ones above.
4) Routine can contain ExercseForRoutine objects (which will look the same as Exercise but will just be used for routines, will not be able to add sets to.
5) ExercseForRoutine will be added to Routine, but not related to the actual Exercise objects that will be used when the user enters workout data, this object is just used to create a Routine.
Any advice on tips and how to structure this using Parse would be appreciated
Don't get too hung up on your object model or your data store. If you haven't done so already, generate a list of core use cases and use those to drive your object model. But assume that you will iterate on it until you find a good fit with your domain. The one thing that sounds like it might be a bit fishy about your spec is writing the single day constraint into your Workout class. That doesn't sound like it's essential to the domain and probably will be awkward to code (e.g., what happens if I start my workout at 11:55pm?).
Related
I am using Core Data to store objects. What is the most efficient possibility for me (i.e. best execution efficiency, least code required, greatest simplicity and greatest compatibility with existing functions/libraries/frameworks) to store different attribute values for each object depending on the context, knowing that the contexts cannot be pre-defined, will be legion and constantly edited by the user?
Example:
An Object is a Person (Potentially =Employer / =Employee)
Each person works for several other persons and has different titles in relation to their work relationships, and their title may change from one year to another (in case this detail matters: each person may also concomitantly employ one or several other persons, which is why a person is an employee but potentially also an employer)
So one attribute of my object would be “Title vs Employer vs Year Ended”
The best I could do with my current knowledge is save all three elements together as a string which would be an attribute value assigned to each object, and constantly parse that string to be able to use it, but this has the following (HUGE) disadvantages:
(1) Unduly Slowed Execution & Increased Energy Use. Using this contextual attribute is at the very core of my prospective App´s core function (so it would literally be used 10-100 times every minute). Having to constantly parse this information to be able to use it adds undue processing that I’d very much like to avoid
(2) Undue Coding Overhead. Saving this contextual attribute as a string will unduly make additional coding for me necessary each time I’ll use this central information (i.e. very often).
(3) Undue Complexity & Potential Incompatibility. It will also add undue complexity and by departing from the expected practice it will escape the advantages of Core Data.
What would be the most efficient way to achieve my intended purpose without the aforementioned disadvantages?
Taking your example, one option is to create an Employment entity, with attributes for the title and yearEnded and two (to-one) relationships to Person. One relationship represents the employer and the other represents the employee.
The inverse relationships are in both cases to-many. One represents the employments where the Person is the employee (so you might name it employmentsTaken) and the other relationship represents the employments where the Person is the Employer (so you might name it employmentsGiven).
Generalising, this is the solution recommended by Apple for many-many relationships which have attributes (see "Modelling a relationship based on its semantics" in their documentation).
Whether that will address all of the concerns listed in your question, I leave to your experimentation: if things are changing 10-100 times a minute, the overhead of fetch requests and creating/updating/deleting the intermediate (Employment) entity might be worse than your string representation.
I'm trying to write an iOS application that tracks how much weight your lifting at the gym but I'm struggling to create my Core Data schema. At the moment i have an Exercise Entity that stores info about a specific exercise (e.g. Bicep Curl) and i have a workout Entity that just has a name & image. The workout entity has a many to many relationship with the exercise entity. what i need to incorporate is 'session' functionality - the ability for a user to complete a workout and store the weights he/she lifted in a particular workout. E.g. i want to say that i completed my 'Leg Day' workout and lifted these weights for each exercise. So my Core Data looks like this at the moment.
schema
How would i go about storing the session data? I feel that the session must have a one to one relationship with a workout but that doesn't let me add results for each exercise in the workout... I also think i might need a dictionary to store the weights for each exercise.
Any help would be greatly appreciated as i have never really learnt about databases before.
Thanks
EDIT: Ive changed my schema to look like this
schema2
So, regarding your schema2, in database design it is typically bad practice to have relationships that form a "circle". Also, if an entity only has a single attribute, it could be reduced to an attribute of its parent.
I would probably approach your design a bit differently.
You should split your exercise entity into Exercise and Exercise_History or something similar. Then you would remove any relationship between Session and Workout and have a one to many between Session and Exercise_History.
Also, if Lift only has a single attribute, it probably shouldn't be its own entity.
If you approach it like that, you should get the desired relationships and functionality.
I've got a table with attributes:
Country, Place, Description etc.
A value of the country attribute can be repeated(one country can repeat two times or more).
I need to get from this DB:
All Countries. (for tableView)
All Places for a particular Country.(for tableView)
Other info for a particular Place. (for details
view)
So, the question is what's the best way to implement the xcdatamodeld.
My thoughts:
I can create just an simple Entity without any relationships. But to
get 1 and 2 points(described below) I have to go through all
ManagedObjects and I thinks it's not thes best way.
To use setPropertiesToFetch:. I think it's a good way but I'm not sure is
it okay to leave my database so simple without any relationships
like(Country -> Place)
I thought to create three entities Country,
Places, Details. And load necessary data from different entities.
But it's my first experience with CoreData and I'm not sure does it
make sense or no?
P.S. This is simple example. Real database can be much more bigger and complicated.
The example is trivial you could start with something as simple as:
This is how you would model it if you was ignoring the database - luckily CoreData will take care of the persistence for you and you can just use your models without thinking too much about tables
You have a concern about the efficiency once you click on a country. You should use an NSFetchedResultsController to handle efficiently batching data into the UI and working with a tableView. You would simply set up the NSFetchedResultsController to use a predicate something like:
[NSPredicate predicateWithFormat:#"country == %#", self.selectedCountry];
Side note:
You should always profile to see if there is a real problem rather than working on opinion of estimation
I do have some JSON file http://jsonblob.com/530664b3e4b0237f7f82bdfa I am pulling from forecast.io.
I am little confused how I should be creating my CoreData entities and relationships.
In below setup, I made my Location entity as the parent entity and created a separate entity for Currently, Minutely, Hourly, Daily. However I have decided it's best to hold all the information regarding the weather data in one entity, so I created a Data table for that purpose and tied it to Daily and Currently in the image below.
Before going further, I paused and would like to get a second opinion on it. Is this a valid way of going forward with this?
EDIT: Based on Wain's response I changed my model to this
Currently Minutely and Hourly add little value as they don't have any attributes or relationships. It's also generally easier to add a type attribute rather than having a number of sub entities because you can easily filter the type using a predicate while doing a fetch. If you're going to add more in the future then there could be a case for keeping sub entities.
Once the entities are trimmed down then you only have a Location and Data with a relationship. You should make that relationship bi-directional so that Core Data can manage the data store contents better. (this applies to all relationships, even if you keep the sub entities you already have).
Other than that, fine :-)
Im about to add the persistence layer to my application, and i decided to give core data a go. Currently i map all my models to entities, which seems to work quite well. But in my current implementation i use something i call "collections" (of models) for example i have a collection of tile slots in a game.
this SlotsCollection class has methods like findNextInSameRow() findAvailableSlot() etc. What ive done with core data is i have created a Game entity and added a to many relationship to the Slot entity, Is there a way to define a class which the collection of slots should be instantiated with so i can put my logic inside that? Or is there a better way for me to structure things. I guess i could create "managers" inside my Game entity and hand in the slots when initialized
SlotManager* manager = [SlotManager alloc] initWithSlots:self.slots];
Slot* slot = [manager findAvailableSlot];
Also after i "migrated" all my models to entities, i have alot of entities that do not have any attributes but only hold references to other entities. Im abit afraid im using a wrong mindset when structuring the core data.
The class that has the collection should have the logic for that collection.
If you have a 1-to-many relationship from A to B, then you'd put the logic about this relationship into class A — and possibly some of it inside class B (depending on your needs).
Note: If you're iterating through relationships, you need to be aware of faulting behavior etc. Whenever Core Data has to do actual database work, you incur a performance hit. That's no different that plain old SQL. If you don't have to "go to disk" things are very fast. If you're using fetch request you will always do database work, and things will always be (relatively) expensive.