0

I have a Core Data model entity NoteObject that has a transformable type arrayOfTags. In the NoteObject.h file, which is a subclass of NSManagedObject, the arrayOfTags is declared as:

NSMutableArray *arrayOfTags;
@property (nonatomic, retain) NSMutableArray *arrayOfTags;
//.m
@dynamic arrayOfTags;

The issue is that changes that are made to this array are not saved. Someone suggested the following as the solution:

If there are mutable and immutable versions of a class you use to represent a property—such as NSArray and NSMutableArray—you should typically declare the return value of the get accessor as an immutable object even if internally the model uses a mutable object.

However I'm not exactly sure what that means. How would I follow those instructions for my case?

4
  • How are you trying to change the data in the array? Commented Mar 5, 2012 at 21:21
  • By adding to it. This is the original question I asked: stackoverflow.com/questions/9556834/… Commented Mar 5, 2012 at 21:22
  • From my understanding of this is that You would copy the NSMutableArray into a NSArray and return the NSArray for use. Commented Mar 5, 2012 at 21:25
  • Can you show me how that would look? Commented Mar 5, 2012 at 21:29

2 Answers 2

1

Even of you've found a workaround in the meantime try this:

[noteObject willChangeValueForKey:@"arrayOfTags"];

// make changes to noteObject.arrayOfTags

[noteObject didChangeValueForKey:@"arrayOfTags"];
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this actually worked too. Which is better, your suggestion, or the [noteObject setArrayOfTags:noteObject.arrayOfTags]?
yep this one is better and which I have used in my projects. As far as I can remember I have copied from Apple sample code. (could be the books example but not sure anyway). EDIT: its 'better' because it doesn't rely on other conditions. Its like with bindings on the Mac, just tells Cocoa that changes were made, and please update all related objects.
1

Implementing accessor functions for Core Data varies with your relationship model. This document should help you get started. Most likely you will be using this setup for your getter:

- (NSArray*)data
{
    [self willAccessValueForKey:@"data"];
    NSArray* array = [[NSArray alloc] initWithArray:arrayOfTags copyItems:YES];
    [self didAccessValueForKey:@"data"];
    return array;
}

Please note that the above snippet is just an example and will have to be modified for your use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.