0

I'm trying to use transformable with my entity class. But I had to do with some transformer? ValueTransformer in Core Data. I found some of them just making their own custom class, but for me I'm trying to use my Entity class generated by CoreData

It's my core data properties code

import Foundation
import CoreData


extension PersonEntity {

  @nonobjc public class func fetchRequest() -> NSFetchRequest<PersonEntity> {
    return NSFetchRequest<PersonEntity>(entityName: "PersonEntity")
  }

  @NSManaged public var gender: String?
  @NSManaged public var id: UUID?
  @NSManaged public var name: String?
  @NSManaged public var volunteered: Bool
  @NSManaged public var schedules: NSSet?
}

// MARK: Generated accessors for schedules
extension PersonEntity {

  @objc(addSchedulesObject:)
  @NSManaged public func addToSchedules(_ value: ScheduleEntity)

  @objc(removeSchedulesObject:)
  @NSManaged public func removeFromSchedules(_ value: ScheduleEntity)

  @objc(addSchedules:)
  @NSManaged public func addToSchedules(_ values: NSSet)

  @objc(removeSchedules:)
  @NSManaged public func removeFromSchedules(_ values: NSSet)

}

extension PersonEntity : Identifiable {

}

And It's my core data class

import CoreData

@objc(PersonEntity)
public class PersonEntity: NSManagedObject, NSSecureCoding {
  public static var supportsSecureCoding: Bool = true
   
  enum Key: String {
    case id = "id"
    case name = "name"
    case gender = "gender"
    case volunteered = "volunteered"
  }
   
  init(id: UUID, name: String, gender: String, volunteered: Bool) {
    self.id = id
    self.name = name
    self.gender = gender
    self.volunteered = volunteered

// I'm getting Error here
// Must call a designated initializer of the superclass 'NSManagedObject'
// Implements How?
    super.init(entity: , insertInto: )
  }
   
  public func encode(with coder: NSCoder) {
    coder.encode(id, forKey: Key.id.rawValue)
    coder.encode(name, forKey: Key.name.rawValue)
    coder.encode(gender, forKey: Key.gender.rawValue)
    coder.encode(volunteered, forKey: Key.volunteered.rawValue)
  }
   
  required convenience public init?(coder: NSCoder) {
    let decodedId = coder.decodeObject(forKey: Key.id.rawValue)
    let decodedName = coder.decodeObject(forKey: Key.name.rawValue)
    let decodedGender = coder.decodeObject(forKey: Key.gender.rawValue)
    let decodedVolunteered = coder.decodeObject(forKey: Key.volunteered.rawValue)

    self.init(id: decodedId as! UUID, name: decodedName as! String, gender: decodedGender as! String, volunteered: (decodedVolunteered != nil))
  }
}
chan
  • 1
  • 1
  • Why would you want to use transformable with your whole class? What is your goal here? – Joakim Danielson Feb 23 '23 at 12:16
  • N:N relation, I try to put those person Entities inside 2d array in core data – chan Feb 23 '23 at 12:34
  • 2
    Then create a proper relationship instead, Core Data supports many-to-many relationships – Joakim Danielson Feb 23 '23 at 12:48
  • but i need to save 2d array in core data – chan Feb 23 '23 at 15:26
  • Why do you need a 2d array, it is really hard to understand what you are trying to do and what the best way forward is. But you should _not_ store an entity as a transformable attribute. – Joakim Danielson Feb 23 '23 at 15:33
  • i'm making schedule app, like part time by team. like [[person1, person2], [person 3,person4]] and these data are dynamic, it can be rearange by user. – chan Feb 23 '23 at 15:36
  • so if it is impossible, should i just make multiple relation like firsttime, secondtime, thirdtime to have multiple person and store person in these relation attributes? – chan Feb 23 '23 at 15:42
  • Create an entity Team? Team, Schedule, Person? Not sure what you have today but a third entity could be a way to handle this. – Joakim Danielson Feb 23 '23 at 15:46

0 Answers0