Predicates in coredata in objectivec - ios

I have two tables in coredata Details and person.
Person has two fields Id and Code:
Id Code
IAS RT
IAS TP
IAS IP
Now,detailshas two fields,code and Name
Code Name
RT Rataz
TP Tranzps
IP Irrz
Now I want to use predicates so that i can first fetch the id from person,then after getting the field against the id.I can use the same id as predicate to get Name from person table.How this can be achieved.Any help or suggestion would be appreciated.Thanks in advance.

OK, how you should do this is using a relationship.
Add a relationship to Person called "details" and add a relationship to Detail called "people" (or "person" if it's singular).
Now you can get the person by the id and then use person.details to get the details associated with that person.
Core Data is not a database. Think of it more as a data model. Create the data model and object relationships that you need and use them.
You would be better off checking out a core data tutorial. It will help you in understanding how to use core data.

Related

NSFetchedResultsController - NSSortDesciptor One-To-Many relationship

In my data model I have a Customer object and each customer can have many Address objects mapped as a one to many relationship.
I am using NSFetchedResultsController to get the data for each Customer. However I want to sort the response by city on the Address object.
Please can you show me how this can be done (if possible ta ll). I've tried a few ways but with no real luck.
Thanks

Delete objects form Core Data

I implemented core data in my app with 2 Entity named "Employees" & "Departments".
They have a relationship with many to many. But no action for deletion now on relationship.
Now, I need like if any department has no employee then that department will automatically delete from DB.
Is this possible?
Thanks for your time :)
It is not possible to delete automatically if we have a trouble to fetch u need to filter the data using predicate matching with your requirements .

Creation of relationship with attribute in CoreData with iOS in Objective C

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.

How to fetch records from a class which has parent class reference?

I'm new to CoreData structure, I have two classes one is "Person.h" and another is "Education.h" which has one to many relations ship Person<--->> Education`.
Here's the attributes for each classes,
Person.h
personID (unique) Number
pName String
pAge Number
educations Set
here, p stands for person
Education.h
educationID (unique) Number
eName String
eState Number
eStarted String
eCompleted String
eCenterName String
eBy Person
here, e stands for education
Ok, now I want to fetch (all / some) education details for a Person. I've successfully inserted records in both the classes with proper inputs. How to get this done? Any suggestion? Please consider me to correcting, even if this flow would not clear to you (or its wrong).
Thanks,
Hagile
Normally you'd have a Core Data relationship on Person that points to the Education entity, configured as to-many. Then once you have an instance of Person, you just look up the value of that relationship like you'd look up the value of any property. You get back a collection of zero or more related Education instances, and you don't need to do an additional fetch.
Your eBy relationship on Education suggests that you're thinking of this as if you were working with SQL. With Core Data it's normal to have a to-many relationship defined on the entity that has the relationship (and indeed, eBy should really have an inverse relationship).

Can a Core Data entity have relation with itself?

Can a Core Data entity have a relation with itself ?
Something like a Self Join ?
No problems. Just create an entity like Person, then you can create a Manager which is a Person, and Coworkers as Many People. I know this example is contrived, but this is straight forward.
Remember that unlike strings which are stored in the object, the Person in manager and coworkers are just pointers.
Yes, this is what the Apple Documentation says:
A relationship specifies the entity, or the parent entity, of the
objects at the destination. This can be the same as the entity at the
source (a reflexive relationship).

Resources