Question

I have a Core Data Model with three entities: Transaction, Occasion, Person and when I'm currently fetching in the Transaction Entity, I am getting "NULL" as the value in the database. Let me explain.

Transaction is in my case, the Entity that has relationships to Person and Occasion. The person entity has a name attribute and a relationship to the transaction entity. transactions (Person Entity) < ------ >> whoBy (Transaction entity).

There is also a relationship from the categories attribute (occasion entity) < ---------------- >> event (Transaction Entity).

I am at the basic stages of this app. I have one Table View Controller and when you press the plus button, you're asked, in a modal view, to add the name and occasion. Eventually both would be displayed in the Table View with the name being the main text of the cell and the occasion being the subtitle.

When I add the inserted values from the user directly to the Person entity, representing the name attribute, it works and I see this in the Table View. The same goes for occasion (separately to Person).

However, when I add the data directly to the Transaction Entity and use a fetchRequest to bring that, every time I press save to pass the value and return to the Table View, I'm presented with (NULL).

My AddEntryVC has this in the save button method:

NSManagedObject *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
[newEntry setValue:self.nameTextField.text forKeyPath:@"whoBy.name"];

In the Table View, I have:

// There is NSMutableArray property called transactions to hold the transactions NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Transaction"]; self.transactions = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];

// In the cellForRowAtIndexPath NSManagedObject *transaction = [self.transactions objectAtIndex:indexPath.row];

[cell.textLabel setText:[NSString stringWithFormat:@"%@", [transaction valueForKeyPath:@"whoBy.name"]]];

When I run this, there are no errors/warnings and when I add an entry, I see (NULL) as the saved result. When I run the same code, with just modifying the Transaction to be the Person directly, including the MutableArray, it works.

So I'm almost there.. but would really appreciate any assistance. I feel the relationship of whoBy.name is not working. Because it's a relationship. I can't imagine I'd have to go set the Person.name attribute; it should just work with the Transaction?

I have also tried a predicate below the fetch request:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"whoBy.name = %@", self.personTransaction.name];

.. Where personTransaction is a @property (nonatomic, strong) Person *personTransaction property and that has the same result.

Thanks in advance

Was it helpful?

Solution

When you operate on a relationship you have to manually create all the entities, e.g. you have to replace:

NSManagedObject *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
[newEntry setValue:self.nameTextField.text forKeyPath:@"whoBy.name"];

with something like:

NSManagedObject *transaction = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
person.name = self.nameTextField.text;
transaction.whoBy = person;

BTW make sure to read: Core Data Programming Guide - Relationships and Fetched Properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top