How to delete parent object after delete children objects without primary key?


#1

Hi ,
I’m trying to add offline feature
everything is working very good

but now after some task I need to delete

for example after I post recID : 4677
I need also delete it from realm file

my realm file is complex

its like this

import Foundation
import RealmSwift

@objcMembers
class RecordsViewResponse: Object,Codable {
    private(set) var details: Details?
    private(set) var attachments = List<Attachment>()

    enum CodingKeys: String, CodingKey {
        case details = "Details"
        case attachments = "Attachments"
    }
}

@objcMembers
class Attachment: Object,Codable {
    private(set) var id = RealmOptional<Int>()
    dynamic var guid: String?
    private(set) var recId = RealmOptional<Int>()
    dynamic var name: String?
    dynamic var attDesc: String?
    private(set) var typeId = RealmOptional<Int>() // if it was 1 that , mean it can be siging
    private(set) var size = RealmOptional<Int>()
    private(set) var pageCount = RealmOptional<Int>()
    private(set) var storageType = RealmOptional<Int>()
    dynamic var filePath: String?
    dynamic var contents: String?
    private(set) var isLetter = RealmOptional<Bool>()
    dynamic var createDate: String?
    dynamic var createdBy: String?
    private(set) var sort = RealmOptional<Int>()
    
    
    enum CodingKeys: String, CodingKey {
        case id = "ID"
        case guid = "GUID"
        case recId = "Rec_ID"
        case name = "Name"
        case attDesc = "Att_Desc"
        case typeId = "Type_ID"
        case size = "Size"
        case pageCount = "Page_Count"
        case storageType = "Storage_Type"
        case filePath = "File_Path"
        case contents = "Contents"
        case isLetter = "IS_Letter"
        case createDate = "CreateDate"
        case createdBy = "CreatedBy"
        case sort = "Sort"
    }
    
    override class func primaryKey() -> String  {
        return "id"
    }

}

@objcMembers
class Details: Object,Codable {
    private(set) var id = RealmOptional<Int>()
    dynamic var guid: String?
    private(set) var recId = RealmOptional<Int>()
    dynamic var recTitle: String?
    dynamic var fromUser: String?
    dynamic var fromUserName: String?
    dynamic var directonText: String?
    dynamic var directonName: String?
    dynamic var createDate: String?
    
    enum CodingKeys: String, CodingKey {
        case id = "ID"
        case guid = "GUID"
        case recId = "Rec_ID"
        case recTitle = "Rec_Title"
        case fromUser = "From_User"
        case fromUserName = "From_User_Name"
        case directonText = "Directon_Text"
        case directonName = "Directon_Name"
        case createDate = "CreateDate"
    }
    
    override class func primaryKey() -> String  {
        return "id"
    }
}

this some data from my Realm file

RecordsViewResponse :

Details :

Attachment:

I did try to delete it like this

 do {
let realm = try Realm()

guard let recordsDetiels = realm.objects(Details.self).filter("recId == %@",recId).first else {return}
                                                                
guard let recordsAttachments = realm.objects(Attachment.self).filter("recId == %@",recId).first else {return}
                                                              
      try realm.write {
         realm.delete(recordsDetiels)
          realm.delete(recordsAttachments)
        }
                                                                
         }catch{
          print("realm Error: \(error)")
           }

it did delete it from Details and Attachment

but in RecordsViewResponse remind Null and empty list
as in this photo