NSCoder - Only Called On Object instantiation - core-data

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

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.

Related

How do I convert an struct of type Data to an object of type CNGroup?

I'm having trouble converting a CNGroup object to a Data object and back to a CNGroup object. I decided to start rethinking the problem again. Somewhere along the way I decided that I should use the Data class to save a CNGroup object to CloudKit. I also learned that the field type to use in my CKRecord object would be of the type bytes.
Am I correct so far?
I am able to convert a CNGroup object to a Data object and back again unless I store the Data object in CloudKit and then retrieve it before I convert the Data object back to a CNGroup object, in which case I get an error when I try to access the pointee property of the typed pointer. That would be an UnsafeBufferPointer, an UnsafeMutableBufferPointer, or an UnsafePointer.
I've tried a lot of different code using different ways. It is impractical to put so much code in my post. I have used the copyBytes method and the withUnsafeBytes method of the Data object.
There is one simple code, and that is when I converted the CNGroup object to a Data object:
func convertCNGroupToData(fromCNGroup group: inout CNGroup) -> Data {
return Data(bytes: &group, count: MemoryLayout.size(ofValue: group))
}
I am looking for a simple way to do what I want. I am relooking at Apple's documentation of Data and NSData.
I am not able to be more specific with this question. I appreciate any effort to help me with this.

How ejs Extract values from object

I am using Sequelize where I fetch all the data from a table using findAll. Which is basically an Array of objects. What seems to be confusing is that the data I am showing as output nested under objects. (Sounds Confusing? Let me clarify)
So, Let's I have this Short Code
Here if I run this code, it will give me undefine because father lies in parent, for which I have to use user.parent.father, Right?
Okay, now on Fetching data from table in my code,
I console.log my first row, for which I get this.
Now here the values which I need lies in dataValues.
In my ejs file. I am using simple for-of loop
Now my Question is why am I not getting undefined for product.title , product.imageUrl and so on? It is supposed to get those data by product.dataValues.title. Because It lies in another object names dataValues.
Technically, when a value is initialized by Sequelize, your object's prototype is set to Model (the class is too long to copy-paste it here).
When you create your model, Sequelize calls init on it (line 424) and which in turn calls refreshAttributes.
This one calls Object.defineProperty to define both the getter and the setter for each property you have defined in the metadata (line 1238).
The getter and setter are set to get and set functions respectively (lines 1095 to 1103).
This actually means that
instance.field
is just a property wrapped over
instance.get('field')
This corresponds with their docs which says
Instance instances operate with the concept of a dataValues property, which stores the actual values represented by the instance. By default, the values from dataValues can also be accessed directly from the Instance, that is:
instance.field
is the same as
instance.get('field')
is the same as
instance.getDataValue('field')

Serializing works but unserializing crashes

I'm trying to set up a save function in my HaxeFlixel game.
Some background: The object in question is an instance of Player, which extends FlxSprite. Save data is stored in an instance of a custom class I made for it. That instance is stored in a StringMap (the keys are save names), which is saved by serializing it to a variable in a FlxSave.
Creating the save data and writing it works fine. However, reading the save data back in crashes the game with the message "Invalid field: pixels". pixels is a field from FlxSprite, but it's not the first such field in the serialized string, so it's probably not that.
If it's useful, the declaration of that field is y6:pixelsn - that is:
y begin a field, which is named...
6: a string of length 6...
pixels (the string)
n null
From this line of code you can see that pixels is actually not a variable* at runtime. So the unserializer would crash when it tries to assign value to pixels. But more investigation is need on why the serializer serialized the pixels fields at the first place, because it shouldn't really exist at runtime.
Note*: the accessors of pixels are (get, set), which makes pixels not a real property at runtime. Read more here.
As a general rule, I don't recommend serializing a FlxSprite (or other complex objects) directly. Rather, you should extract the desired information (e.g. x/y position or hp, etc) and serialize only those.

Storing object in Esent persistent dictionary gives: Not supported for SetColumn Parameter error

I am trying to save an Object which implements an Interface say IInterface.
private PersistentDictionary<string, IInterface> Object = new PersistentDictionary<string, IInterface>(Environment.CurrentDirectory + #"\Object");
Since many classes implement the same interface(all of which need to cached), for a generic approach I want to store an Object of type IInterface in the dictionary.
So that anywhere I can pull out that object type cast it as IInterface and use that object's internal implementation of methods etc..
But, as soon as the Esent cache is initialized it throws this error:
Not supported for SetColumn
Parameter name: TColumn
Actual value was IInterface.
I have tried to not use XmlSerializer to do the same but is unable to deserialize an Interface type.Also, [Serializable] attribute cannot be used on top of a Interface, so I am stuck.
I have also tried to make all the implementations(classes) of the Interface as [Serializable] as a dying attempt but to no use.
Does any one know a way out ? Thanks in advance !!!
The only reason that only structs are supported (as well as some basic immutable classes such as string) is that the PersistentDictionary is meant to be a drop-in replacement for Dictionary, SortedDictionary and other similar classes.
Suppose I have the following code:
class MyClass
{
int val;
}
.
.
.
var dict = new Dictionary<int,MyClass>();
var x = new MyClass();
x.val = 1;
dict.Add(0,x);
x.val = 2;
var y = dict[0];
Console.WriteLine(y.val);
The output in this case would be 2. But if I'd used the PersistentDictionary instead of the regular one, the output would be 1. The class was created with value 1, and then changed after it was added to the dictionary. Since a class is a reference type, when we retrieve the item from the dictionary, we will also have the changed data.
Since the PersistentDictionary writes the data to disk, it cannot really handle reference types this way. Serializing it, and writing it to disk is essentially the same as treating the object as a value type (an entire copy is made).
Because it's intended to be used instead of the standard dictionaries, and the fact that it cannot handle reference types with complete transparency, the developers instead opted to support only structs, because structs are value types already.
However, if you're aware of this limitation and promise to be careful not to fall into this trap, you can allow it to serialize classes quite easily. Just download the source code and compile your own version of the EsentCollections library. The only change you need to make to it is to change this line:
if (!(type.IsValueType && type.IsSerializable))
to this:
if (!type.IsSerializable)
This will allow classes to be written to the PersistentDictionary as well, provided that it's Serializable, and its members are Serializable as well. A huge benefit is that it will also allow you to store arrays in there this way. All you have to keep in mind is that it's not a real dictionary, therefore when you write an object to it, it will store a copy of the object. Therefore, updating any of your object's members after adding them to the PersistentDictionary will not update the copy in the dictionary automatically as well, you'd need to remember to update it manually.
PersistentDictionary can only store value-structs and a very limited subset of classes (string, Uri, IPAddress). Take a look at ColumnConverter.cs, at private static bool IsSerializable(Type type) for the full restrictions. You'd be hitting the typeinfo.IsValueType() restriction.
By the way, you can also try posting questions about PersistentDictionary at http://managedesent.codeplex.com/discussions .
-martin

How to auto-generate early bound properties for Entity specific (ie Local) Option Set text values?

After spending a year working with the Microsoft.Xrm.Sdk namespace, I just discovered yesterday the Entity.FormattedValues property contains the text value for Entity specific (ie Local) Option Set texts.
The reason I didn't discover it before, is there is no early bound method of getting the value. i.e. entity.new_myOptionSet is of type OptionSetValue which only contains the int value. You have to call entity.FormattedValues["new_myoptionset"] to get the string text value of the OptionSetValue.
Therefore, I'd like to get the crmsrvcutil to auto-generate a text property for local option sets. i.e. Along with Entity.new_myOptionSet being generated as it currently does, Entity.new_myOptionSetText would be generated as well.
I've looked into the Microsoft.Crm.Services.Utility.ICodeGenerationService, but that looks like it is mostly for specifying what CodeGenerationType something should be...
Is there a way supported way using CrmServiceUtil to add these properties, or am I better off writing a custom app that I can run that can generate these properties as a partial class to the auto-generated ones?
Edit - Example of the code that I would like to be generated
Currently, whenever I need to access the text value of a OptionSetValue, I use this code:
var textValue = OptionSetCache.GetText(service, entity, e => e.New_MyOptionSet);
The option set cache will use the entity.LogicalName, and the property expression to determine the name of the option set that I'm asking for. It will then query the SDK using the RetrieveAttriubteRequest, to get a list of the option set int and text values, which it then caches so it doesn't have to hit CRM again. It then looks up the int value of the New_MyOptionSet of the entity and cross references it with the cached list, to get the text value of the OptionSet.
Instead of doing all of that, I can just do this (assuming that the entity has been retrieved from the server, and not just populated client side):
var textValue = entity.FormattedValues["new_myoptionset"];
but the "new_myoptionset" is no longer early bound. I would like the early bound entity classes that gets generated to also generate an extra "Text" property for OptionSetValue properties that calls the above line, so my entity would have this added to it:
public string New_MyOptionSetText {
return this.GetFormattedAttributeValue("new_myoptionset"); // this is a protected method on the Entity class itself...
}
Could you utilize the CrmServiceUtil extension that will generate enums for your OptionSets and then add your new_myOptionSetText property to a partial class that compares the int value to the enums and returns the enum string
Again, I think specifically for this case, getting CrmSvcUtil.exe to generate the code you want is a great idea, but more generally, you can access the property name via reflection using an approach similar to the accepted answer # workarounds for nameof() operator in C#: typesafe databinding.
var textValue = entity.FormattedValues["new_myoptionset"];
// becomes
var textValue = entity.FormattedValues
[
// renamed the class from Nameof to NameOf
NameOf(Xrm.MyEntity).Property(x => x.new_MyOptionSet).ToLower()
];
The latest version of the CRM Early Bound Generator includes a Fields struct that that contains the field names. This allows accessing the FormattedValues to be as simple as this:
var textValue = entity.FormattedValues[MyEntity.Fields.new_MyOptionSet];
You could create a new property via an interface for the CrmSvcUtil, but that's a lot of work for a fairly simple call, and I don't think it justifies creating additional properties.

Resources