3
votes

I have two entities, Document and Attachment. A document can have many attachments. An attachment can belong to a single document.

For some reason, Core Data is not saving the relationship between a document and its attachments when I do the following:

[document addAttachments:attachments];
[context save:&error];
[context reset];

If I fetch the document after executing the above code, its attachments will be empty. Yet, the attachments are saved.

The relationship is saved if I set the inverse relationship manually:

[document addAttachments:attachments];
for (Attachment *attachment in attachment)
{
    attachment.document = document;
}
[context save:&error];
[context reset];

I thought it wasn't necessary to set a relationship both ways with Core Data. What am I doing wrong?

In case it helps, the relationships are defined as follows:

attachments

  • Transient: NO
  • Optional: YES
  • Destination: Attachment
  • Inverse: document
  • Delete Rule: Cascade
  • Type: To Many
  • Ordered: NO

document

  • Transient: NO
  • Optional: YES
  • Destination: Document
  • Inverse: attachments
  • Delete Rule: Nullify
  • Type: To One

Also tried

  • Using mutableSetValueForKey: instead of the generated accessor.
  • Adding the attachments one by one.
  • Retaining the attachments array, just in case.

Same result.

1
I'm having the same issue. Were you ever able to find a solution?blwinters

1 Answers

0
votes