How to retrieve a specific object through a relationship in core data - objective-c

Say I have 2 managed objects in my model: Department and Employee (as discussed in the Core Data Programming Guide). If I already have a specific department retrieved, I know I can get all employees in that department through
NSSet *departmentsEmployees = aDepartment.employees;
but what if I want to find a specific employee (e.g. with employeeId = 123) in that set, change one of its attributes, then save the change? How do I do that? Can I do a targeted query on the set? Or would I have to loop through each employee to find the one I want?
It seems that it would be better to try to find it in the employees NSSet instead of doing a whole new query to the entire data model because I already have a specific department.
Thank you

One way to narrow the search is to use -[NSSet filteredSetUsingPredicate:].

Related

Coupling between classes

Let's assume we have two classes: Patient and MedicalExamination. We want to get all examinations for a specific patient. Which one is better:
patient.getExaminations();
examination.get(patient);
How would you implement the second way? It would force you to loop over all examinations and see which one are about your patient.
The first method however will allow each patient to hold its own examinations and thus immediately retrieve it. Therefore my preference goes to this method.
Give the object as much information as you reasonably should (without lowering security concerns) so it can operate on its own.
Basically, an examination cannot exist without a patient. Therefore Patient would be the root entity of the aggregate Patient - MedicalExamination, and as such Patient (or PatientRepository) would be the way to retrieve patient details and examinations:
patient.getExaminations();
A doctor usually have a file for each patient, in each patient's file, there is a list of medical examination.
So, you get the patient from the doctor office, then you get the examination from the patient's file.
so:
patient.getExaminations();

Core Data two degrees relationships bindings in NSTableView

I have a Core Data model made like this :
Persons <->> Jobs <<-> Society
A Person can have multiple Jobs, each one in a different Society.
Societies can have multiple Jobs.
Jobs can be related only to one Person and one Society.
Here is the picture of the Core Data Model :
I want to be able to put the Job and a NSComboBox containing all my Societies, with the good one selected, in a NSTableView (View based). Here is the picture of my cell when one Person is selected :
For the moment I manage to bind the Person to their Jobs, and to get the Society related to the Job, but I'd like to get the whole Societies NSArrayController objects and select the one related to the specific job...
It's not completely clear to me what you're looking to do, but here if you're just trying to select the right society cell based on a job, this is what you could do:
To get all of the Societies, you should just make a fetchRequest without a predicate, e.g.:
NSArray *societies = [managedObjectContext executeFetchRequest:[NSFetchRequest fetchRequestWithEntityName:#"Society"] error:&error];
To select the right Society, you can just do a comparison in the tableViewCell by comparing job.society and the cell's society, or you could check to see whether the job is in the society.jobs set
There may be a way to do all of this by finding a count with a subquery in one fetch, but if that is even possible it's going to be a hard fetch to write. It's also unnecessary by the sound of it.

How to determine number of children of a record?

Good afternoon everyone!
I'm studying NHibernate, and decided to make some changes. Among them, I noticed that some fields are unnecessary. So I bring my doubt:
I have a list, let's call it Class_List within each study class, I can have N students for each class. Within the list Class_List, I also have other properties as simple as the name of the class.
How I see it is unnecessary to store how many students I have in the database, I would, in a single query, how many records I have. This, using NHibernate.
Is this possible? How?
Best regards,
Gustavo.
Edit: I've forgot to say one thing... I want to return this number of record, as a column. But this column is not mapped in my .hbm.xml file.
If students are mapped as a collection on Class, you can try using something like this:
var numberOfStudents = session.CreateCriteria<Class>()
.Add(Restrictions.IdEq(1))
.CreateCriteria("_students", "students")
.SetProjection(Projections.RowCount())
.UniqueResult<Int32>();
Where '1' is the id of the class (you can use other property) and '_students' is the name of the students collection.

retrive data using multiple table in core data in iPhone

Actually I have three tables as empName(empId,empName),empSalary(empId,salary),empBonus(salary,bonus).
And I have a name and need to find the bonus.
So My logic is that first find using name I will find the empId then using empId find the salary and finally using salary find the bonus from empBonus table.
How can I achieve following things in core data in iPhone.
Use relationships between data, so that whenever you find the name, it also finds the salary and bonus from the other tables. See here
I would not do raw sqlite queries if you have this level of knowledge of Core Data and Sql.
First of all you should stop thinking in tables. Core Data is not a wrapper for SQlite.
If you want tables don't use Core Data and use SQLite.
Core Data is an object graph and persistence framework. You have entities instead of tables.
Because you are talking about tables I have no idea what you actually want but your Data Model could look like this:
or this:
(the Employee->Salary relationship in the last screenshot should be to-many, I forgot to put that into the model.)
and because you have an object graph you can get all the objects in that graph.
For the last example model your code could look like this:
Employee *myEmployee = /* do a fetch to get an Employee */
NSSet *salaries = myEmployee.salaries;
Salary *salary = /* choose one from the salaries set */
Bonys = salary.bonus;

tests for data access (sql queries) functionality

I want to know the best way of testing data access functionality.
I know that it is possible to create mocks to change data layer objects that is using to test business logic.
However is it possible to test if sql queries to database are correct.
Scenario: A method must return employees which were applied for work last month.
I can return list of object and check if each employee's startDate property is correct (last month).
So if it returns 3 employees and they have correct startDate value but there are more two employee in database which aren't returned. How to write test for that case? :)
Thanks in advance.
You set up the test DB so you shold know what data is in it. If you expect 5 employees to be returned from the query and you get only 3, you know there is an error.
You can test the query with different setups: empty table, only new employees, only old employees, a mix of the two (with special care to the borderline cases), etc.
I don't think you need to check the two other employees in the database which aren't returned.
The key is, when setting up your test data, you would want to ensure you have enough records that don't match the criteria (in addition to the records that do match), then you run the fetch and make sure you get back the correct number of records that do match the criteria.
Preparing the test data in this manner ensures that your method is returning the expected results.