I am trying to store custom class type array in coredata.
What I've got setup is an Entity called Node
with properties value
of type string and children
of type Transformable
. The children
property is supposed to store an array of type Node
as in the code below.
public class Node: NSManagedObject {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Node> {
return NSFetchRequest<Node>(entityName: "Person")
}
@NSManaged public var children: [Node]?
@NSManaged public var value: String?
}
.xcdatamodel
setup:
Currently the app crashes with error message "This decoder will only decode classes that adopt NSSecureCoding. Class 'Node' does not adopt it." So I tried it with String array instead of Node array and it seems to work.
I'm guessing something extra needs to be done to store Custom array type.
It sounds like you want a recursive data structure, where each node can have many other nodes as children and another node as its parent. This is what CoreData relationships are for.
Under your
Node
entity:children
of type "To Many" with destinationNode
. No inverse.parent
of Type "To One" with destinationNode
, set its inverse tochildren
children
relationship and set its inverse toparent
children
, meaning if you delete a parent all of its children are deleted too. "Nullify" makes sense forparent
, meaning if you delete a child it just removes the parent's connection to that one child.