0

i'm trying to save my API response in my core data entity. I have some attributes that have custom classes. These custom classes are from third party library which i cannot make a change. This is my entity which contains custom class attributes, enter image description here

When i try to save the response in my entity like this,

let appDelegate = UIApplication.shared.delegate as? AppDelegate
        let context = appDelegate?.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "SingleChatCoreData", in: context!)
        let user = NSManagedObject(entity: entity!, insertInto: context)

        for items in dateWiseSortedSingleRooms
        {
            user.setValue(items.name, forKey: "name")
            user.setValue(items.actualNameFor_1_2_1_chat, forKey: "actualNameFor_1_2_1_chat")
            user.setValue(items.isGroup, forKey: "isGroup")
            user.setValue(items.lastMsgRead, forKey: "lastMsgRead")
            user.setValue(items.lastMsgTimeActual, forKey: "lastMsgTimeActual")
            user.setValue(items.lastMessage, forKey: "lastMessage")
            user.setValue(items.lastMsgTime, forKey: "lastMsgTime")
            user.setValue(items.profilePic, forKey: "profilePic")
            user.setValue(items.roomSID, forKey: "roomSID")
            user.setValue(items.isNewGroup, forKey: "isNewGroup")
            user.setValue(items.unReadMsgsCount, forKey: "unReadMsgsCount")
            user.setValue(items.unReadMsgsCount, forKey: "unReadMsgsCount")
            user.setValue(items.members, forKey: "members")
            user.setValue(items.messages, forKey: "messages")
            user.setValue(items.twChannelObj, forKey: "twChannelObj")
        }

        do {
            try context?.save()
            print("Saved successfully.")
        } catch  {
            print("Fail to save")
        }

The app crashes with an error message in console,

error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x28389ff20> , -[TCHMember encodeWithCoder:]: unrecognized selector sent to instance 0x2809f75c0 with userInfo of (null)

I have searched for the error and i just to come to result that NSCoding protocol should be used . Then i have used NSCoding protocol in my NSManagedObjecClass for the entity like this,

import CoreData

import TwilioChatClient

@objc(SingleChatCoreData) public class SingleChatCoreData: NSManagedObject,NSCoding {

public required convenience init?(coder aDecoder: NSCoder) {

    let name = aDecoder.decodeObject(forKey: "name") as? String
    let roomSID = aDecoder.decodeObject(forKey: "roomSID") as? String
    let isGroup = aDecoder.decodeBool(forKey: "isGroup")
    let lastMessage = aDecoder.decodeObject(forKey: "lastMessage") as? String
    let lastMsgTime = aDecoder.decodeObject(forKey: "lastMsgTime") as? String
    let lastMsgTimeActual = aDecoder.decodeObject(forKey: "lastMsgTimeActual") as? String
    let profilePic = aDecoder.decodeObject(forKey: "profilePic") as? String
    let lastMsgRead = aDecoder.decodeBool(forKey: "lastMsgRead")
    let unReadMsgsCount = aDecoder.decodeInt32(forKey: "unReadMsgsCount")
    let actualNameFor_1_2_1_chat = aDecoder.decodeObject(forKey: "actualNameFor_1_2_1_chat") as? String
    let isNewGroup = aDecoder.decodeBool(forKey: "isNewGroup")
    let twChannelObj = aDecoder.decodeObject(forKey: "twChannelObj") as? TCHChannel
    let members = aDecoder.decodeObject(forKey: "members") as? [TCHMember]
    let messages = aDecoder.decodeObject(forKey: "messages") as? [TCHMessage]

self.init(name:name!,roomSID:roomSID!,isGroup:isGroup,lastMessage:lastMessage!,lastMsgTime:lastMsgTime!,lastMsgTimeActual:lastMsgTimeActual!,profilePic:profilePic!,lastMsgRead:lastMsgRead,unReadMsgsCount:Int16(unReadMsgsCount),actualNameFor_1_2_1_chat:actualNameFor_1_2_1_chat!,isNewGroup:isNewGroup,twChannelObj:twChannelObj!,members:members!,messages:messages!)

}


public func encode(with encoder: NSCoder) {
    encoder.encode(name, forKey: "name")
    encoder.encode(roomSID, forKey: "roomSID")
    encoder.encode(isGroup, forKey: "isGroup")
    encoder.encode(lastMessage, forKey: "lastMessage")
    encoder.encode(lastMsgTime, forKey: "lastMsgTime")
    encoder.encode(lastMsgTimeActual, forKey: "lastMsgTimeActual")
    encoder.encode(profilePic, forKey: "profilePic")
    encoder.encode(lastMsgRead, forKey: "lastMsgRead")
    encoder.encode(unReadMsgsCount, forKey: "unReadMsgsCount")
    encoder.encode(actualNameFor_1_2_1_chat, forKey: "actualNameFor_1_2_1_chat")
    encoder.encode(isNewGroup, forKey: "isNewGroup")
    encoder.encode(twChannelObj, forKey: "twChannelObj")
    encoder.encode(members, forKey: "members")
    encoder.encode(messages, forKey: "messages")

}

init(name:String,roomSID:String,isGroup:Bool,lastMessage:String,lastMsgTime:String,lastMsgTimeActual:String,profilePic:String,lastMsgRead:Bool,unReadMsgsCount:Int16,actualNameFor_1_2_1_chat:String,isNewGroup:Bool,twChannelObj:TCHChannel?,members:[TCHMember],messages:[TCHMessage]) { let appDelegate = UIApplication.shared.delegate as? AppDelegate let context = appDelegate?.persistentContainer.viewContext let entity = NSEntityDescription.entity(forEntityName: "SingleChatCoreData", in: context!)

    super.init(entity: entity!, insertInto: context)
    self.name = name
    self.roomSID = roomSID
    self.isGroup = isGroup
    self.lastMessage = lastMessage
    self.lastMsgTime = lastMsgTime
    self.lastMsgTimeActual = lastMsgTimeActual
    self.profilePic = profilePic
    self.lastMsgRead = lastMsgRead
    self.unReadMsgsCount = unReadMsgsCount
    self.actualNameFor_1_2_1_chat = actualNameFor_1_2_1_chat
    self.isNewGroup = isNewGroup
    self.twChannelObj = twChannelObj
    self.members = members
    self.messages = messages
}

} Now ,again when i try to save the response it crashes on same point with same error, I'm stuck to this issue more than a week, kindly can anyone let me know why this issue is so what the mistake i'm doing?

Junaid Khan
  • 125
  • 2
  • 13
  • Don't repost questions. As I said in the duplicate both classes must adopt `NSCoding` to be able to be saved as transformable. But the best solution would be to map the custom classes to Core Data entities. – vadian Feb 26 '19 at 11:02
  • I cannot edit that classes because they are third party classes and they are inherit from NSObject class, this is what it shows for classes, /** Representation of a Member on a chat channel. */ interface TCHMember : NSObject . @vadian – Junaid Khan Feb 26 '19 at 11:05
  • But you can map the classes to Core Data entities. – vadian Feb 26 '19 at 11:06
  • How it can be done please help in that. @vadian – Junaid Khan Feb 26 '19 at 11:09
  • Create two entities with attributes matching the properties of the classes and add logic to *copy* to attributes (in both directions for encoding and decoding). – vadian Feb 26 '19 at 11:11
  • can you give an example for this? @vadian – Junaid Khan Feb 26 '19 at 11:15
  • I don't know the contents of both custom classes. If you are using a separate entity the connection must be a relationship rather than an attribute. Please read the Core Data Programming Guide. – vadian Feb 26 '19 at 11:23
  • Suppose i make seprate entity for both classes and their attributes in them, then how i call them in my this entity. @vadian – Junaid Khan Feb 26 '19 at 11:31
  • As I said you have to establish a Core Data Relationship between the objects. – vadian Feb 26 '19 at 11:37
  • Objects means entities? @vadian – Junaid Khan Feb 26 '19 at 11:43
  • Yes, objects means entities. – vadian Feb 26 '19 at 11:44
  • okay let me try with this, thanks for your support. @vadian – Junaid Khan Feb 26 '19 at 11:56

0 Answers0