0

Here's what I have:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"NoteObject" inManagedObjectContext:appDelegate.managedObjectContext];

[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(parentNoteId == %@) AND (noteId!=rootNoteId)", appDelegate.currentNoteId];
[fetchRequest setPredicate:predicate];
NSError *error;

[appDelegate.arrayOfNotes setArray:[appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

NoteObject *note=[appDelegate.arrayOfNotes objectAtIndex:0];
[note.arrayOfTags addObject:someObject];

NSManagedObjectContext *context = [appDelegate managedObjectContext];
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

The fetch works fine, and adding an object to the arrayOfTags works and reflects in the UI. However, when I exit the app and come back, the arrayOfTags is missing the one that I added (but it has the other two, so I know the array is working properly). It's not saving for some reason.

(The NoteObject is a subclass of NSManagedObject, and arrayOfTags is a transformable property of the entity.)

Am I doing something wrong here?

Edit: Here's how I add a new note, which saves just fine, even with the arrayOfTags, and everything is saved when I exit the app and come back in.

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *noteEntity = [[appDelegate.managedObjectModel entitiesByName] objectForKey:@"NoteObject"];
NoteObject *tempNote = [[NoteObject alloc] initWithEntity:noteEntity insertIntoManagedObjectContext:context];
[tempNote.arrayOfTags addObject:@"tag1"];
NSError *error;
        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }

It's only when I make changes that it's not saved properly..

2
  • Just a string..I'm not sure yet, but I think my problem might be that I'm not using a fetchedResultsController Commented Mar 4, 2012 at 18:09
  • It seems more likely to be a problem in your arrayOfTags implementation - can you include more detail about that? Commented Mar 4, 2012 at 18:13

2 Answers 2

0

Did you mean you are using a Transformable attribute? Does this help: Core Data saving problem: can't update transformable attribute (NSArray)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this is the same problem. I dont understand his solution though. What exactly did he do?
0

Turns out all I had to do was re-set the array after making changes to it:

NoteObject *tempNote = [[NoteObject alloc] initWithEntity:noteEntity insertIntoManagedObjectContext:context];
[tempNote.arrayOfTags addObject:@"tag1"];

[tempNote setArrayOfTags:tempNote.arrayOfTags]; //this is the magic line

NSError *error;
if (![context save:&error]) {
       NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

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.