I have a bunch of entities in Core Data and I'm looking for a way to link them together into what I think is called a "View" in Sqlite for easy access.
For example...
Table 1: "Events"
EventId
VenueId
ArtistId
Table 2: "Venues"
VenueId
Name
City
Table 3: "Artists"
ArtistId
Name
Genre
Views: "MyEvents"
EventId
ArtistName
ArtistGenre
VenueName
VenueCity
isRegistered (property within table - doesn't fetch)
When I create a row to "MyEvents" with the EventId, it should fetch the data from the "Artist" table and the "Venue" table that correspond to that EventId.
Thoughts?
The word you are looking for is "Relationships".
Check out Apple's documentation or this tutorial.
Unfortunately there is no concept of a "View" in core data. There aren't really "Tables" either. Every records is an, "Entity" - which is essentially a Row in the "That Entity" table. And each "Entity" has attributes (Columns). Entities can have a "Relationship" (Foreign Key) based on their "Attributes" (Columns). When you perform a fetch a relationship behaves like a join.
To get the behavior you want you would have to add foreign keys or make add entities to relate your entities together: e.g. relate the "Events" and "Venues" entities by having an "EventVenueRelationship" entity with attributes "EventId" and "VenueId" and corresponding relationships.
I'm working on a similar problem at the moment where I have a db structure which is very federated and works great on the server, but is ridiculous when you try to shoehorn it into CD. Did you find a better way to solve this?
Related
I would like to create a model with two entities Orders and Products. They are linked by a relationship 'Contain' that has an attribute 'quantity'. How can I represent that in CoreData ? (Do not send me the ray tutorial, or any tutorial on youtube, I think I have done every thing). It is very important the relationship with attribute and not something general. (I know that it is not a database, but it is a Conceptual data model/Conceptual Schema as it is named by "entities" and "relationship" so if there is relationships, there must be a way to have relationships with attribute).
EDIT :
Am I doing the right thing by not adding id_order and id_product to the Contain entity ?
In CoreData, Contain would be another entity. It would have relationships to Orders and Products and a quantity attribute.
You cannot add attributes to a relationship in CoreData.
I am new to core data just started learning the new ideas in core data.
I have core data database which has three entity Student,Department and an entity for Mapping Student and department.Let name it as StudentDepartment
Student will have all student details with a primary key studentID
Department will have department details with a primary key departmentID
StudentDepartment will have studentID and DepartmentID as foreign key.
Multiple student can be enrolled in a department and a same student can be enrolled to multiple department.
How to create this schema in core data.
If am deleting a studentID in student table subsequent row should be deleted in StudentDepartment table. Similarly if am deleting departmentID in department table subsequent rows should be deleted in StudentDepartment.How to make this relationship by using core data.
Please provide me a xcmodel.
CoreData isn't a database, it's an object store that happens to (sometimes) be implemented on top of a relational database.
The practical result of that is that you really don't need to explicitly create a separate table for relationship mapping. Instead you create your two entities and then create a relationship between the two. From your description, it sounds like you want a many-to-many relationship between the two. At an implementation level, core data will magically create the needed relationship table.
Additionally, you can establish a delete-rule for each side of the relationship that mandates what to do when an item is deleted. Pin this case, you'll want to set the delete rule for both to nullify, which will break the relationship when either end is deleted.
I have Orders, Items and OrderItems entities with n:n relationship between Orders and Items table. I am using Fluent Hibernate for mapping, in "Order" entity there is "List" property which has "protective set". I am facing below problem, while inserting.
new Order.Items.Add(alreadyexistingitem);
Since its already existing item, in the orderitems table, a record needs to be inserted into orderitems table with new orderid and existing item id. But only new order details are inserted in order table not in associative table.
"Inverse" attribute is set for "items" bag in orders entity. So, I need to attach the newly added order while adding item like below but how can i do it as it is the list object with protective set.
Orders.Items.Add(alreadyexistingitem, *order = "neworder"*);
Below is the mapping:
model.Override<Order>(m =>
{
m.HasManyToMany(c => c.Items)
.Table(Constants.TABLE_PREFIX + "OrderItem")
.Inverse()
.Cascade.AllDeleteOrphan();
});
model.Override<Item>(m => m.HasManyToMany(c => c.Orders)
.Table(Constants.TABLE_PREFIX + "OrderItem")
.Cascade.None());
As a preliminary answer, NHibernate is smart enough to know when the same object reference is being used in two places. All you should need is a "HasManyToMany" relationship in the mapping between Order and Item.
Inverse should probably not be used on the orders side. Inverse tells NHibernate that the opposite side of the relationship has the power to define and break the relationship. Orders have items; items don't get to "choose" what order they belong to.
Im planning on using CoreData to model a “News” iOS app. This’ll have many sections (read a UITabBarItem) which will contain many articles (in a UITableView). The articles might appear in other sections too. Also the sort order of each article is another important factor.
In database lingo I'd have
"Article" table
Sections" table and a
"Article_Sections" table to map an article to a section with additional sort order field.
I have tried and can’t model this in CoreData. Im stuck with a
“Section” class having a many-to-many relationship with
a “Article” class (with an inverse many-to-many relationship to the “Section” class).
However with this I don’t know how to derive the sort order. Any hint on how to do this is very much appreciated. Thanks in advance.
Sandeep
I had to stop thinking in terms of database to sort this out. My solution involved 2 Classes:
ArticleList
Properties:
* List name
* Article Id
* Sort order
Relationship:
* Article ; one-to-one
Article
Properties:
* Article Id
* ....
Relationship:
* List ; one-to-many
I query the ArticleList and sort on sortOrder to get a list of articles for a particular feed/section. Then use the valueForKeyPath: to fetch article properties (such as title etc).
I think that you need to remove the direct relationship between section and article and just have a relationship from your Article_Sections table back to the section and article tables.
You can add an entity "ArticleSectionPair" as an intermediary between "Section" and "Article" with:
an attribute "sortValue"
a one-to-many relationship "section" to the "Section" entity (with its reverse "articlePairs")
a one-to-many relationship "article" to the "Article" entity (with its reverse "sectionPairs")
Using the attribute "sortValue" and th, you can get a sorted collection of ArticleSectionPairs from a section and get the articles with [sortedArticlePairs valueForKeyPath:#"article"].
I have 3 tables in my core data tables.
Item table: items, which has an ID column and a connection to a properties table.
Properties table: it has a propertyValue column and a connection to item table and a connection to property table.
Property table: it has a propertyName column and a connection to properties table.
The property table contains a propertyName called "price".
The properties table contains a propertyValue "20" for the property "price".
Do you think I can sort the Items table by price?
I am using a NSFetchedResultsController and I am creating a NSFetchRequest for it.
I have tried to write a NSSortDescriptor with a comparator block object for the NSFetchRequest. It isn't working. After this I tried to write a NSSortDescriptor without any selector or block object, I just setup a key called "dealPrice" and created a category on the Item managed object with a method called - (NSString *)dealPrice. It wasn't working neither.
Do you know any other method? Or do you know the solution?
You've obviously got a bad case of SQL fever. Your trying to treat Core Data like an SQL wrapper and that is messing everything up.
Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.
A Core Data datamodel should not be configured depending on the needs of the UI or any other non-data requirement. Instead, it should accurately model/simulate the real world objects, events or conditions that the app deals with.
In this case, you are modeling:
A type of property that has a name and a price.
An item denoted by an id of some kind
A relationship between one or more particular property instances and one or more instances of item.
Therefore, your data model only needs two entities connected by a relationship. You don't need a "join" because the relationship handles the connection between the two entities automatically.
The simplest model has just a one-to-one relationship:
Item{
id:string
property<-->Property.item
}
Property{
name:string
price:number
item<-->Item.property
}
If each Item object can have several associated Property objects then you would have:
Item{
id:string
properties<-->>Property.item
}
Property{
name:string
price:number
item<<-->Item.properties
}
If each Property object can have several associated Item objects:
Item{
id:string
property<<-->Property.items
}
Property{
name:string
price:number
items<-->>Item.properties
}
How you configure your sort descriptors depends on the details of the relationships and which entity's objects your tableview will display.
What I would first recommend is to stop thinking about CoreData like a database. It is NOT a database. The things you call "tables" are actually Entities. Think of them as objects, that have properties and relationships to other objects. Think about making your data model as simple as possible. Do not try to optimize your structure for database performance etc. The actual backing schema is not under your control.
With that in mind, from what you've posted about your data model, it seems like you should be able to collapse into at least 2 entities instead of 3 (perhaps 1 but not sure without seeing your entire data model). Then, you should be able to do a simple fetch on the Items entity with a predicate that sorts on a property of it's related object.
It sounds like your real object model is and entity named Deal with an attribute named "price".