contentful / contentful-persistence.swift

Simplified persistence for the Contentful Swift Library.
MIT License
23 stars 35 forks source link

Sync not removing invalid relationships? #122

Open GabrielHauber opened 3 years ago

GabrielHauber commented 3 years ago

This is the problem I'm having, and I think it's on the Contentful sync side…

The problem is this:

  1. Content synced where model A has relationship to model B
  2. Later, content is updated, removing the relationship between A & B. Both A & B content still exist though, just the relationship is removed
  3. Next sync, the relationship still exists in the synced data. I can't see anything in the code that would remove this relationship?
GabrielHauber commented 3 years ago

Ok it turns out to have been a CoreData setup issue. The persistence SDK was doing the right thing in setting the new relationships, and even examining them before / after they were right, but the problem was in merging from background context to the view context.

i.e. Sync was performed on a background context using the default merge policy to the view context. This meant that the changes were merged, not overwriting, leaving both old & new relationships on a to-many entity relationship.

The fix was explicitly setting persistentContainer.viewContext.mergePolicy = NSMergePolicy.overwrite before performing the sync in the background.