Skip to content

Add or update child Table in 1 to 1 relationship #465

Open

Description

@dwirandytlvk

Hallo @JohnEstropia
I'am trying this library that seems great, i want to ask about how add / update unique object

for example i have 2 object FlightBookingInfo has one FlightBookingDetail, i want to update FlightBookingInfo and also FlightBookingDetail from my json that i got from the API, i can add or update FlightBookingInfo but FlightBookingDetail value is inserted instead of update existing value that has relationship to FlightBookingInfo

May i know how to update FlightBookingDetail from json, without fetching it first from database?

FlightBookingInfo

class FlightBookingInfo: CoreStoreObject, ImportableUniqueObject {
    
    typealias UniqueIDType = String
    
    static var uniqueIDKeyPath: String = String(keyPath: \FlightBookingInfo.$invoiceId)
    
    typealias ImportSource = [String: Any]
    
    @Field.Stored("invoiceId")
    var invoiceId: String = ""
    
    @Field.Stored("bookingStatus")
    var bookingStatus: String = ""
    
    @Field.Stored("rescheduleId")
    var rescheduleId: String = ""
    
    @Field.Relationship("flightBookingDetail", inverse: \.$flightBookingInfo, deleteRule: .cascade)
    var flightBookingDetail: FlightBookingDetail?
    
    static func uniqueID(from source: [String : Any], in transaction: CoreStore.BaseDataTransaction) throws -> String? {
        return source["invoiceId"] as? String
    }
    
    func update(from source: [String : Any], in transaction: CoreStore.BaseDataTransaction) throws {
        self.invoiceId = source["invoiceId"] as? String ?? ""
        self.bookingStatus = source["bookingStatus"] as? String ?? ""
        self.rescheduleId = source["rescheduleId"] as? String ?? ""
        self.flightBookingDetail = try transaction.importObject(Into<FlightBookingDetail>(), source: source)
    }
}

FlightBookingDetail

class FlightBookingDetail: CoreStoreObject, ImportableUniqueObject {
    
    static func uniqueID(from source: [String : Any], in transaction: CoreStore.BaseDataTransaction) throws -> String? {
        return source["invoiceId"] as? String
    }
    
    typealias UniqueIDType = String
    
    static var uniqueIDKeyPath: String = String(keyPath: \FlightBookingInfo.$invoiceId)
    
    func update(from source: [String : Any], in transaction: CoreStore.BaseDataTransaction) throws {
        let flightBookingDetailSource = source["flightBookingDetail"] as? [String: Any] ?? [:]
        multipleAirlines = flightBookingDetailSource["multipleAirlines"] as? Bool ?? false
        twoWay = flightBookingDetailSource["twoWay"] as? Bool ?? false
        originDate = flightBookingDetailSource["originDate"] as? Date
        returnFlightDate = flightBookingDetailSource["returnFlightDate"] as? Date
    }
    
    
    typealias ImportSource = [String: Any]
    
    @Field.Stored("multipleAirlines")
    var multipleAirlines: Bool = false
    
    @Field.Stored("twoWay")
    var twoWay: Bool = false
    
    @Field.Stored("originDate")
    var originDate: Date?
    
    @Field.Stored("returnFlightDate")
    var returnFlightDate: Date?
    
    @Field.Relationship("flightBookingInfo")
    var flightBookingInfo: FlightBookingInfo?
}

Here is how i add/update the data

      let jsonData: [String: Any] = [
            "invoiceId": "1111",
            "bookingStatus": "\(["failed", "success", "pending"].randomElement()!)",
            "rescheduleId": "\(Int.random(in: 1000..<2000))",
            "flightBookingDetail": [
                "multipleAirlines": Bool.random(),
                "twoWay": Bool.random(),
                "originDate": Date.now,
                "returnFlightDate":  Calendar.current.date(byAdding: .day, value: 1, to: Date.now),
            ]
        ]
        
        CoreDataManager.instance.dataStack?.perform(
            asynchronous: { transaction in
                try transaction.importUniqueObject(Into<FlightBookingInfo>(), source: jsonData)
            }, success: { _ in
                print("add / update FlightBookingInfo success")
            }, failure: { _ in
                print("add / update FlightBookingInfo failed")
            }
        )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions