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

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

Open
tkirby opened this issue Feb 13, 2023 · 1 comment
Labels

Comments

@tkirby
Copy link

tkirby commented 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?

@JohnEstropia
Copy link
Owner

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
  }    
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants