I have a Core Data object called Car that has a one-to-many relationship with another Core Data object called Claims (i.e., one Car has many Claims). I've created the entities in the data model editor, and am able to list, edit, and work with the Car objects.
What I can't figure out is this: given a Car, how to get an array of all related Claim objects. I think it requires a fetch request, but how does it work?
Thanks!
If the entities are related, you'll want to add a relationship to your models. In core data relationships are two-way, so Car would have a to-many relationship called "claims" and Claim would have a to-one relationship called "car".
Having these relationships in place will give an instance of Car an NSSet of claim objects.
Related
In the tutorials im following for learning about the entity framework, they keep mentioning entities. I often see it gets used as a synonym for the dbsets<> in the database context class, but what's the literal meaning of it?
I already know how the entity framework works, I just dont understand the meaning of the word.
In Entity Framework an entity is largely equivalent to a class in the conceptual model (or the class model, which is mapped to the store model).
In domain model terms an entity is
An object that is not defined by its attributes, but rather by a thread of continuity and its identity.
(Source: Wikipedia)
That quite a mouthful for "an object with an identity", as opposed to a value object, like a DateTime or (maybe) an Address. A Customer is an entity, because it is identified by "who" he is. Two customers with the same name are still two customers.
So entities can loosely be defined as the "things" the business domain is about. The things both the customer/user and the system designer/developer talk about in ubiquitous language. And in EF those things are represented by classes.
So it's not the DbSet. The DbSet is a repository that provides entity objects.
I often see people referring to entities as models. I don't know the origin of this terminology (it seems to happen too often to be a coincidence), but I don't think it's correct. It's mostly confusing. The model in EF is either the store model or the conceptual model, so it's a collection of entities. A model can also be a view model that comprises any number of attributes of any number of entities.
Lets take a Person object for example and lets say the Person data is being posted to a database and its moving through the tiers
When its in my UI, I call it a Person Model or ViewModel.
When its in my business layer I call it a Person Business Object.
When its in my Data Layer, I call it a Person Entity.
Its the same data that is moving into different objects in different tiers. The entity is just the name of the object that is holding the Person data in the Data Access tier....
An entity is simply an object that represents some form of relational data. This is typically used for representing relational databases, but it is not confined to that. I suggest looking at http://msdn.microsoft.com/en-us/data/aa937709 for a brief overview of how the Entity Framework works.
I want to create a one to many relationship. My setup is something like:
I have the Profile Entity,
I have the Time entity.
Every profile has a relationship to Time.
How can I define relationships, and add multiple Time entities to a single profile?
I bet it is obvious, but I can't see how to implement.
edit after posting the answer I saw your comment - to define a to-many relationship in the modeller, select the relationship and choose to-many from the options:
To populate the relationship, you can do it two ways. I am assuming your Profile entity has a to-many relationship called times and the inverse relationship is a to-one relationship called profile.
Set the profile on each Time entity as you are creating them. This will automatically populate the inverse relationship (i.e. add the Time to the times set of the profile).
Collect the relevant Time entities in a set and set the times property to this set. Again, the inverse will be automatically populated.
There is more information here. Accessor methods to add individual entities to a to-many relationship can be generated from the managed object model editor in Xcode.
I am new to the core data and am loving it so far, I just have a question on to-many relations and their inverses. I'm trying to create something where each unit can convert to many other units so each unit can point to many converters that point to just one other unit. The image below works perfectly, but I know that core data wants them to be inverse and lets me know. When I try to select this it eliminates the functionality I desire.
I have tried creating a new relationship on each entity to act like an inverse but it fails, I can get a set of null objects.
Basically I am trying to create a graph-like structure in core data.
Is it possible to get this functionality while making core data happy with supplying inverses?
You should be able to make inverses by adding a relationship to each entity. The Converter entity would get a ConvertsFrom to-one relationship which is inverse to the Converters relationship, and Unit would get a ConvertedBy to-many relationship (since it can be converted by many converters) which is inverse to the ConvertsTo relationship.
What's the point of a fetched property? Does someone have a good example what this is good for?
Some of the uses for fetched properties:
Ordered relationships. The Core Data to-many relationship models a (unordered) set. You can provide the appearance of an ordered relationship using a fetched property with a sort descriptor (assuming there's a natural ordering to the elements).
Cross-store relationships. Relationships in Core Data cannot span persistent stores. You can simulate such relationships using fetched properties, for example storing the objectIDs of the destination in a transformable property and fetching the elements of that collection of objectIDs. Obviously true cross-store relationships are problematic because there's no way to guarantee the presence of an other store at access time.
Filtered relationships. Although fetched properties are not "live' (like iTunes' smart playlists), they can be used to easily filter members of one or more relationships according to attributes (persistent or transient) of any object in the connected object graph.
I have two entities that are connected through a one-to-many relationship, let's say CategoryEntity and ItemEntity. The relationship is optional for CategoryEntity (there can be categories without items), but required for every ItemEntity. At the app's loading, the Categories already exist in the store, and I want to import ItemEntities and connect them to the appropriate CategoryEntity.
Obviously executing a FetchRequest for each ItemEntity to find the matching category wouldn't be a good solution because there will be about 4000-6000 Items each time..
So, is there something more efficient I could do?
If you have correctly setup your Core Data model, then you have a to-many relationship from the Category entity to the Item entity, and an inverse to-one relationship from Item to Category. Also, you have a "cascade" delete rule for the to-many relationship and a "nullify" delete rule for the to-one relationship.
Assuming this, each time you insert an Item object, setting its Category relationship automatically inserts the Item into the corresponding Category. Deleting an Item automatically removes it from the corresponding Category.
On the Category side, removing a Category automatically removes all of the corresponding Item objects.
Therefore, when you fetch Items, you have already fetched for each Item object its corresponding Category object. You do not need do anything else. Note that, by default, you are not actually retrieving the Category object during the fetch: instead a fault is fired the first time you try to access the object and the object is retrieved at that time. This provides better performances if you do not plan to use immediately the Category object stored within the Item object just fetched. If you plan to use the Category object almost every time you fetch an Item, then you must use the NSFetchRequest methods
- (void)setReturnsObjectsAsFaults:(BOOL)yesNo
- (void)setRelationshipKeyPathsForPrefetching:(NSArray *)keys
to tell Core Data that you do now want faults and that you ask for prefetching your Category relationship.
When you say 'import' item entities, what do you mean? Are these in another Core Data store, defined in another format in a file somewhere, retrieved over the network?
One approach would be to fetch all the categories in one go and add them to an NSDictionary acting as a cache and keyed by some identifying value that allows you to perform a quick lookup. For each item entity that you instantiate during import (whatever that means), retrieve its category ID and then retrieve the Category MO from the cache. Set the relationship and then save. Even better, batch up a number of insertions and save every 10, 100 or 1000 to reduce IO overhead.