Core Data delete only the relationship link between Many-to-Many - not any objects - swift

This is driving me nuts and I can't find anything where someone else has asked the same question. It can't be an unusual situation.
I have a Student entity and a Classes entity and both are bi-directional
Student<<-->>Classes
A Student can be assigned to many Classes and Classes can have many students
When a Student drops out of a given class, I want to remove the link between the particular student and the class - but without deleting either the student or the class objects.
All my attempts have deleted the student or the class object.
How do I remove the relationship without removing the objects ?

Thank You to Joakim Danielson ! The answer was there... I just wasn't aware... Core Data creates a func for the entity relationship.
As an example
entity Students has a relationship named Classes (destination = Classes entity)
entity Classes has a relationship named Students (destination = Students entity)
Core Data creates a func for each of the entity relationships:
.removeFromClasses()
.removeFromStudents()
these remove the relationship link from each of the respective relationships.
In order to remove the relationship completely, I execute both func - the entity objects both remain but are no longer linked !

Related

how to create a many to many relationships with core data? Swift, xcode

I have 2 entities.
entity 1 - People
entity 2 - books
People entity has a property which is an array of string names of their favorite books.
I need to create a relationship that somehow maps the favorite book of a person to the corresponding book entity object(s).
I am not sure how to do this.
So far I have started by creating a relationship in core data model for people by setting destination to "books" and then for the books entity creating a relationship by setting destination to "people".
I don't see or understand how this will automatically pick out each of the person's favorite books...at the end of the day they are both seperate objects. How will the people class know that for a specific people instance that this, this and this book are that person's favorite?
Person attribute as array of string names of books -- very bad idea!
You need a to-many relationship with the Book entity. That's it.
Person <------------>> Book
Then, to get an array of book titles for a particular person:
(person.books as! Set<Book>).map { $0.title }
The person can have an additional to-one relationship (e.g. called favoriteBook) to one of the books.

Entity Framework Model First and Code First TPH issue

I'm facing two problems by trying to make a model first or code first using TPH concept.
The problem is that I need to use table per hierarchy at three levels, so that:
When I use Model First, the last hierarchy entity (third level) does not saves in database. I create an instance from this entity which inherits an abstract entity, which inherits another abstract entity. The data of two abstract entities are saved, but the last entity not saves. If the inheritance goes at maximum two levels works fine.
If I try to use Code First the problem is that I cannot share attributes with same name, for example: ClassB and ClassC has a property named "Name", and both inherits ClassA. When I map to generate database, I want to create only a sql table called ClassA, but it does not share the column "Name", it creates Name and Name1 columns.
I need to do one of this models works, otherwise I can't use inheritances in my model.
Hope some help!
Thanks

Coredata relationship entity creation

I have a question about the coredata relationship.
Essentially, if I have 1 entity called i.e parent and the other entity which it has a relationship with is children and it is inverse.
If I create the parent entity, will it create the children entity as well? I 've set the "parentchildrenrelationship" to optional but it looks like every time I create the parent entity, it creates the children entity.
Is that something normal ? Thanks
It is not normal. Are you sure you're not creating the children entity otherwise? Just because you create one entity does not mean it creates the entities that it is referencing.
Core Data should not automatically create entities to fulfill relationships, especially if the relationship is optional. The value of children should either be nil (for a one-to-one relationship) or an empty NSSet or NSArray (for a one-to-many relationship, depending on ordering) for a newly created parent entity.

CoreData One-to-One Relationship

In my app, Im using CoreData and I have two entities. VenueInfo and ContactInfo.
In the app, you can add venues, and each venue has ContactInfo. So I've setup a one to one relationship in my model.
So I would imagine, I could simply do the following:
[venue.contact setValue:textField.text forKey:email];
So like so you'd set the email attribute of the contact object which belongs to the venue. However this doesn't work.
Is it possible because the contact object doesn't exist yet?
The line you wrote won't work that way. I assume contact is the relationship name you have in your Venue entity in xCodeModel.
[venue.contact setValue:textField.text forKey:email]; // can't write this way..
Suppose your ContactInfo entity has two fields : phone, email so this way you can go.
and VenueInfo entity has two fields : name.
VenueInfo *venueInfo=[NSEntityDescription insertNewObjectForEntityForName:#"VenueInfo" inManagedObjectContext:self.managedObjectContext];
vanueInfo.name=txtVenueName.text; // venue name entry
ContactInfo *contactInfo=[NSEntityDescription insertNewObjectForEntityForName:#"ContactInfo" inManagedObjectContext:self.managedObjectContext];
contactInfo.phone=txtPhone.text;
contactInfo.email=txtEmail.text;
contactInfo.venue=venueInfo; // I assume venue is the relationship name you give in ContactInfo entity towards Venue entity.
What we did here is we saved phone and email details to ContactInfo entity only and then we just related it with currently selected VenueInfo accessing the relationship we declared in ContactInfo Entity for VenueInfo.
This maybe quite confusing so I'll recommend you to follow a few tutorials which will give you an idea to grasp some basics about core data relationships.

iPhone Development - Query related records using CoreData

I have a case where i have three entities with one-to-many and one-to-many relationships:
Entity A (Entity B relationhip),
Entity B (Entity A relationship, Entity C relationship),
Entity C (Entity B relationhip)
I have the reference of Entity A, and now i want to fetch all the related Entity C records. How can i do that? (with least amount of code)
Edit: Here's another way to put it.
Can we perform joins with CoreData. For example, (and this is a very crude example), We have a following entity-relationship:
Grand Parent (1)---(m) Parent
Parent (1)---(m) Child
So, now if i have "Albert" the Grand Parent, and i want to get all his grand children, how can i do that?
In case someone else stumble across a similar situation, here's what worked for me:
NSArray *allFieldValues = [myEntityA valueForKeyPath:#"Entity B relationship.Entity C relationship.requiredFieldInEntityC"];
I was mainly interesting in reading the data of a single field in Entity C (that's linked to myEntityA object). The key concept here is that "don't think of CoreData as a 'database'".