Snowman
Snowman

Reputation: 32061

Core Data : Updating array.

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?

Upvotes: 0

Views: 154

Answers (2)

Seb
Seb

Reputation: 3564

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.

Upvotes: 1

Felix
Felix

Reputation: 35384

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

[noteObject willChangeValueForKey:@"arrayOfTags"];

// make changes to noteObject.arrayOfTags

[noteObject didChangeValueForKey:@"arrayOfTags"];

Upvotes: 1

Related Questions