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
trytransaction.importUniqueObjects(Into<Hotel>(),sourceArray:jsonArray.map({.hotelJSON($0)})// or .hotelPriceJSON depending on the method)
Then in your ImportableUniqueObject implementation:
publicfuncupdate(fromsource:ImportSource,intransaction:BaseDataTransaction)throws{switchsource{case.hotelJSON(letjson):// parse as how you expectcase.hotelPriceJSON(letjson):// 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
}
}
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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?
@JohnEstropia commented on GitHub (Feb 14, 2023):
Sure, I do something like this a lot in my projects:
then in your import code:
Then in your
ImportableUniqueObjectimplementation: