I have an CoreData model with the entity Person and an attribute called name. I store 4 strings into name (Pete, Paul, Dan and Jo). Now I'd like to find out which indexPath Paul has. How can I do this?
My CoreData model:
To display Core Data entities in a table view, you should use a NSFetchedResultsController. One requirement is that the entities you fetch must be sorted, so the index path is unequivocal.
To find the index path of a Person object in your fetched results controller driven table view, use
let indexPath = self.fetchedResultsController.indexPathForObject(person)!
Related
Suppose I saved the Following Restaurant Details like id, name, address like that in core data , then how can i check wether Restaurant with particular id exist or not in core data data base.
Create an NSFetchRequest for the appropriate entity.
Assign an NSPredicate to the request with format id == %#, currentID.
Perform a fetch.
If the returned array is not empty the item exists.
I have some points on a map with associated informations contained by Core Data, to link the points on the map with the associated info, I would like to have an ID for each point, so in my entity, I would need some ID property, I have read that Core Data has it's own IDs for every managed object but I'm wondering wether or not it would be a good approach for me to directly use them or if I should create my own ID system ?
If you think I should create my OWN ID system, how would I do that ?
Thank you.
CoreData is not an relational database and you should avoid thinking about own ID’s. You may need them only for syncing purposes with external databases. For more precise answer, you should write how your model looks.
[edited after comment]
I don't see that you need any relations. Let's sat you have MapPoint entity with lat, and long properties. If there is only one user note, you just add another property to it like notes. If you have many informations (many records) stored with one MapPoint you need to add Notes entity with properties note and mappoint and make a relation between them. When you insert new Notes object into CoreData you set mappoint property to already existing MapPoint object (fetched after user tap).
I have two entities Employee and Department. Both are related with Relationship i.e., 1-to-M and M-to-1. Dept is having attribute called MANAGERID representing the employee Id who is the manager of that dept.
Now i need to fetch DeptName and the Manager Name. How to write the Predicate Format String ?
NOTE: I am working in iOS 8.3 with Xcode 6.3 and Swift 1.2
Thanks in Advance.
If you follow the usual setup described in Apple's documentation, there is no need for ids. To access the department of an employee
let department = employee.department
To get all employees of a department
let employees = department.employees // returns NSSet or [Employee]
Following the design pattern of object graphs, rather than holding a manager id in the Department object, you should just have a one-to-one relationship with the manager.
let manager = department.manager // returns an Employee
This means that you need a separate relationship for the manager:
Department.employees <----->> Employee.department
Department.manager <------> Employee.managedDepartment
Also note that an all-caps attribute name like MANGAERID is illegal in Core Data.
So I've got a Client entity that needs a relationship to a PhoneNumber entity to allow multiple phone numbers. And I've got an Employee entity that also needs a relationship to a PhoneNumber entity to allow multiple phone numbers. Should I create two separate PhoneNumber entities or can I somehow use the same entity for both?
I would create a parent entity called Person for your Client and Employee entities. The Person entity would have a relationship to the PhoneNumber entity.
Inherited entities have the same attributes and relationships as their parent entity. Of course you can add attributes and relationships to the "child"-entities as well. I omitted that in the screenshot.
Something like this:
you can configure the parent entity in the core data inspector in the right side pane.
I have an object in core data which contains an NSArray filed. This is an array of categories (just strings).
ANd i need to get all objects for certain category. So if an object has an array of categories "film", "music" and i need to get all the film objects - this object is what i need.
so how to write fetch request which wiil search for the value in some array field?
You should consider creating another object in your model called Category. Create relationships between the two objects. Make both of them To-Many relationships.
In your Category object create a string attribute called name. This will be where you set it to film, music, etc. You should then be able to pull back all the objects that match the film category using a predicate such as this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY categories.name == %#", category];
Please watch from Standford Uni. CS193p Video Lectures, the Core Data is explained greatly