mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-13 20:53:32 +01:00
What am I missing in regards to ImportableUniqueObjects, to avoid it inserting a new row every time? #163
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @JakeSteam on GitHub (Sep 5, 2017).
So, I have a pretty simple database, with 5-6 tables that need to sync with remote JSON data. They all use
id(Int64) as a primary key.As the later versions of CoreStore permit using the
.xcdatamodelddirectly, I opted for that method to avoid duplicating schemas. As such, I'm extending my model classes to obey theImportableUniqueObjectprotocol. Inserting, querying etc all works fine, the only issue is with update vs insert.I'm very much a novice Swift developer, so there's likely something very obvious I'm missing. Perhaps extensions can't be utilised in this manner?
Expected behaviour: If a row already exists for that
id, the values for that row are updated. If not, a new row is inserted.Observed behaviour: A new row is always inserted, regardless of whether the
idis in use already.This is called via
transaction.importObjects(Into<Website>(), sourceArray: jsonArray), wherejsonArrayis a JSON array containing something similar to https://pastebin.com/4707LPKk@JohnEstropia commented on GitHub (Sep 5, 2017):
Hi, can you show your import code?
@JakeSteam commented on GitHub (Sep 5, 2017):
Replied before I could even edit post to include it!
Just calling
transaction.importObjects(Into<Website>(), sourceArray: jsonArray), wherejsonArrayis a JSON array containing something similar to https://pastebin.com/4707LPKk@JohnEstropia commented on GitHub (Sep 5, 2017):
Ah, try to use
importUniqueObjects()instead@JakeSteam commented on GitHub (Sep 5, 2017):
Perfect! Knew it would be something super simple. Might want to update the documentation to mention it, thanks so much for your extremely swift help!
@JohnEstropia commented on GitHub (Sep 5, 2017):
Thanks, will mention in the docs (and maybe dump a runtime warning)