0

I have set up a class that conforms to NSCoding, and works as expected when I first create the object. It is essentially the same set up as this

The problem I am having is that the properties of the object when changed are not kept.

For example,

Foo is created and has a property called name.

Foo.name = @"bar"

I can encode / decode the object and it retains the name bar.

If I try and change

Foo.name = @"newName"

The encode method is not called again, so foo.name remains as @"bar" (I have a log state within the encode method)

Also,

I am using a core data object, that has a transformable property which points to the foo object.

Thanks

Community
  • 1
  • 1
DogCoffee
  • 19,820
  • 10
  • 87
  • 120

1 Answers1

1

To "save" the object, you have to call the encode method, e.g. to write it to disk or send it to an output stream.

However, since you are using Core Data to persist the object, you have to call

[managedObjectContext save:&error];

to persist the object after changing it.

That being said, I do not think it makes a lot of sense to have a transformable property that points to a custom class that keeps a string property. Instead, you should think of a more appropriate data structure so you only need transformable properties for non standard data types that cannot be persisted by using the standard data types already built into Core Data.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • thanks, I should add that I am saving the MOC and its still not saving the transformable obj. The main reason for doing this as I wanted to try and conform to the open close principle. Eg, I had a new custom classes with out the need to keep changing the Core Data Model. – DogCoffee Dec 05 '13 at 01:15
  • Did you try calling `archiveRootObject:` to call the encoder routines? – Mundi Dec 05 '13 at 08:34
  • not sure how to call that method, as the location of the object is just a reference from a core data object.... think what I'm trying to do is more hassle than what its worth thanks for help. – DogCoffee Dec 05 '13 at 09:23
  • I would concur. You could accept this answer based on the last paragraph. – Mundi Dec 05 '13 at 14:44