Reputation: 1018
I am trying to store custom class type array in Core Data.
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.
Upvotes: 2
Views: 382
Reputation: 22375
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 destination Node
. No inverse.parent
of Type "To One" with destination Node
, set its inverse to children
children
relationship and set its inverse to parent
children
, meaning if you delete a parent all of its children are deleted too. "Nullify" makes sense for parent
, meaning if you delete a child it just removes the parent's connection to that one child.Upvotes: 2