سؤال

My application has two entities:

  • People
  • Contract

Related like so:

  • One person can have many contracts.
  • A contract can only be held by one person.

I've got the basic application working with the creating of Core Data information to display this in a table view. I am confused about what to do in certain situations, like renaming People.

Suppose Person A has Contract A and Person B has Contract B. If I rename Person A to Person C, then automatically Contract A is assigned to Person C, and therefore Person A does not have any contracts and so should not exist anymore.

How do I achieve that efficiently?

In my app, I have two table view controllers in a tab bar, the first consists of the Contracts as section headers and the People names as the row text label. The second tab consists of no sections, but just rows of people names.

If in the first table view, I have Person A under Contract A and then I go in and rename Person A to Person C, then when the detailViewController saves and the table refreshes, I will see Contract A assigned to Person C successfully and Person A removed.

However, if I go to the People tab, I will still see Person A but I will now also see Person C. Person C, when I click on it shows me Contract A, but Person A has no records when I click on it and should therefore be deleted.

A few things to note:

1) If Person A has OTHER contracts then it should not of course be deleted. 2) I'm using NSManagedObject Subclasses for Person and Contracts 3) Contracts Tab is using Contracts Entity in the NSFetchedResultsController and People Tab is using People Entity in its NSFetchedResultsController. 4) Originally both sides of my relationship were set to Nullify but I tried this with Cascade on one side and then both sides with the same results.

I don't have any code at this point because I'm honestly not sure how to tackle this particular scenario (keeping in mind, everything else is working well up to this point).

هل كانت مفيدة؟

المحلول

I'm not quite sure I understand what you try to accomplish, but ...

It seems that what you are doing is not "renaming" but "re-assigning" a contract to a new user.
Meaning, you are creating "Person C" and assign "Contact A" to it.

I'm not sure what is the behaviour you try to implement.

If you like a Person with no Contracts simply to not appear in your view controller, but still exist in your store, you will need the following:
In your "person view controller" use this predicate for the fetched results controller:

NSPredicate* p = [NSPredicate predicateWithFormat:@"contracts.@count != 0"];

if you like to delete a Person with no Contracts you will need to add custom logic.
This might look something like:

//This goes in your `Person` entity implementation file
- (BOOL) validateForUpdate:(NSError *__autoreleasing *)error
{
    if (![self hasFaultForRelationshipNamed:@"contracts"] && [self.contracts count] == 0) {
        [self.managedObjectContext deleteObject:self];
    }
    return [super validateForUpdate:error];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top