jayhickey/Cirrus

API question

MartinP7r opened this issue · 3 comments

Hi @jayhickey,

first off thank you for providing this package! It already helped me a lot.

I have a quick question about your API:
Is there a reason behind delete and upload accepting Arrays

public func delete(_ models: [Model]) {

while the model changes are passing Sets?

case deleted(Set<CloudKitIdentifier>)

Cheers,
Martin

Hey there, I'm glad to hear you find this helpful 😀

Good question, yes there is a reason: with an array you have the flexibility to call upload and delete with multiple records/models that have the same identifier. The ordering means CloudKit can queue the individual record changes and know what the most recent model is for a given identifier. If these were sets you would get undefined behavior if you tried to upload multiple models with the same identifier (unless you did something like mark the identifier as your sole equality comparison for the set, which is probably not a good idea).

Making them sets on the model change side is primarily for easy of use and to avoid the need to dedupe models with the same identifier on the receiving end. When you receive changes you will just get the latest version of a given model, so order doesn't really matter.

Thank you for responding so quick with a detailed explanation!

I didn't really think of passing multiple records for the same identifier. Thank you for clearing that up.
This probably ties into buffering multiple records with the same id into your uploadContext when the network is unavailable, etc.?
(Sorry if that makes no sense, I haven't dug to far into the source code, yet)

Yes exactly, you're spot on. Offline queuing is one possible use case where you could encounter this situation of trying to upload multiple records with the same identifier.

Feel free to re-open this issue if you have any additional followup questions around this!