0

I have an NSManagedObject subclass where absences is an NSMutableArray

@interface Record : NSManagedObject
@property (nonatomic, retain) id absences;
@end

I want to be able to add items to the absences array; however, if I do [myRecord.absences addObject:SomeObj the record does not save properly. It almost appears that the NSManagedObject does not know that I updated the absences array.

Nevertheless, if I add SomeObj to some localAray, then set myRecord.absences = localArray, the NSManagedObject saves correctly.

Can someone explain this behaviour and how I might avoid it...thanks

Nosrettap
  • 10,940
  • 23
  • 85
  • 140

1 Answers1

1

You're exactly right, in the first case you're changing an object outside of NSManagedObject field of view. To solve this, Apple doc says the following

For mutable values, you should either transfer ownership of the value to Core Data, or implement custom accessor methods to always perform a copy.

So declaring your property with (copy) should suffice.

Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
  • So in `Record` I would do: `@property (nonatomic, retain, copy) id absences;`? Am I understanding correctly that adding the word `copy` means that whenever you try to set this property it will copy the value rather than just setting it directly? – Nosrettap Jan 31 '13 at 05:11
  • You replace `retain` with `copy`, and yes your value will always be copied. – Dmitry Shevchenko Jan 31 '13 at 05:16