Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dwirandytlvk opened this issue Dec 22, 2022 · 0 comments
Open

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

dwirandytlvk opened this issue Dec 22, 2022 · 0 comments

Comments

@dwirandytlvk
Copy link

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")
            }
        )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant