Is it possible to have multiple ImportableUniqueObjects for the same NSManageObject? #403

Open
opened 2025-12-29 15:31:01 +01:00 by adam · 1 comment
Owner

Originally created by @tkirby on GitHub (Feb 13, 2023).

I have two web fetches that update the same object at different times.

Hotel <- HotelJSON
Hotel <- HotelPriceJSON

Both JSONs use HotelId as the unique key. Is it possible to make those both unique objects? Prrotocol can only be declared on Hotel once. Is there a workaround?

Originally created by @tkirby on GitHub (Feb 13, 2023). I have two web fetches that update the same object at different times. Hotel <- HotelJSON Hotel <- HotelPriceJSON Both JSONs use HotelId as the unique key. Is it possible to make those both unique objects? Prrotocol can only be declared on Hotel once. Is there a workaround?
adam added the question label 2025-12-29 15:31:01 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 14, 2023):

Sure, I do something like this a lot in my projects:

class Hotel: CoreStoreObject, ImportableUniqueObject {
  // ...
  enum ImportSource {
    case hotelJSON([String: Any])
    case hotelPriceJSON([String: Any])
  }
}

then in your import code:

try transaction.importUniqueObjects(
  Into<Hotel>(),
  sourceArray: jsonArray.map({ .hotelJSON($0) }) // or .hotelPriceJSON depending on the method
)

Then in your ImportableUniqueObject implementation:

public func update(
  from source: ImportSource,
  in transaction: BaseDataTransaction
) throws {
  switch source {
    case .hotelJSON(let json):
      // parse as how you expect
    case .hotelPriceJSON(let json):
      // parse as how you expect
  }    
}
@JohnEstropia commented on GitHub (Feb 14, 2023): Sure, I do something like this a lot in my projects: ```swift class Hotel: CoreStoreObject, ImportableUniqueObject { // ... enum ImportSource { case hotelJSON([String: Any]) case hotelPriceJSON([String: Any]) } } ``` then in your import code: ```swift try transaction.importUniqueObjects( Into<Hotel>(), sourceArray: jsonArray.map({ .hotelJSON($0) }) // or .hotelPriceJSON depending on the method ) ``` Then in your `ImportableUniqueObject` implementation: ```swift public func update( from source: ImportSource, in transaction: BaseDataTransaction ) throws { switch source { case .hotelJSON(let json): // parse as how you expect case .hotelPriceJSON(let json): // parse as how you expect } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#403