CoreData (+ iCloud) produces invalid model state

brutella

I'm using CoreData with iCloud to sync data between devices.

After every iCloud import (observing NSPersistentStoreDidImportUbiquitousContentChangesNotification), I run a simple de-duplication algorithm to find and delete duplicated data. After saving the changes I see warnings in the console from CoreData that all properties and relationships of a specific entity instance (which was deleted by the de-dupe algorithm) are replaced with nil/0.

CoreData: warning: An NSManagedObjectContext delegate overrode fault handling behavior to silently delete the object with ID '0xd000000000040006 <x-coredata://ADDDABCD-4891-4DCF-B55B-53AA64D11922/<ENTITY_NAME>/p1>' and substitute nil/0 for all property values instead of throwing.

The problem is that one relationship in this entity is not-optional which produces errors the next time iCloud wants to import those changes on other devices.

-[_PFUbiquityRecordsImporter operation:failedWithError:](979): CoreData: Ubiquity:  Import operation encountered had trouble importing log file, Error Domain=NSCocoaErrorDomain Code=134302 "The operation couldn’t be completed. (Cocoa error 134302.)" 
[...], an error occurred saving changes to the persistent store mutated during the import process. [...] 
"The operation couldn’t be completed. (Cocoa error 1560.)"} User Info: { [...] 
NSValidationErrorObject=<NSManagedObject: 0x1742c7bd0> (entity: <ENTITY_NAME>; [...] 
{NSValidationErrorKey=<NON-OPTIONAL_RELATIONSHIP_NAME> [...] 
"Error encountered while importing transaction log at URL: ...

How can I avoid that all properties are set to nil/0?

brutella

How I solved it.

My biggest mistake was how I handled the NSPersistentStoreDidImportUbiquitousContentChangesNotification notification. I only merged the changes in my "main" context (used in the main thread). But I forgot to merge the changes into my second "root" context (of type NSPrivateQueueConcurrencyType) as well which I use to save the context into the persistent store and is the parent of the "main" context.

Without knowing the internals, I suspect that because my "root" context don't know about the ubiquitous content changes, the next time I save changes the resulting transaction logs (written to iCloud) were inconcistent. Now I merge the change notification into my "root" and "main" context and things start to work better.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Coredata, NSOrderedSet and iCloud

From Dev

Issue with CoreData and photos in iCloud

From Dev

CoreData app not syncing with iCloud

From Dev

Save CoreData to iCloud (Swift)

From Dev

Issue with CoreData and photos in iCloud

From Dev

iCloud sync fails with "CoreData: Ubiquity: Invalid option: the value for NSPersistentStoreUbiquitousContentNameKey should not contain periods"

From Dev

How to save changed state of dynamic created UISwitch in CoreData model in Swift?

From Dev

How to save changed state of dynamic created UISwitch in CoreData model in Swift?

From Dev

Model state invalid when using custom RegexAttribute

From Dev

Duplicating CoreData records with iCloud sync

From Dev

iOS 9 CoreData / ICloud - No such document at URL

From Dev

How to get started with SQLite, iCloud and CoreData?

From Dev

Trouble with iOS 8 CoreData iCloud sync consistency

From Dev

iOS 9 CoreData / ICloud - No such document at URL

From Dev

File uploading causes invalid model state in asp.net mvc

From Dev

File uploading causes invalid model state in asp.net mvc

From Dev

Select Box Not Populated On View After Model State Is Invalid

From Dev

iCloud & CoreData: Notification when changed to iCloud Store (First Launch with existing iCloud Data)

From Dev

Putting Logic in a CoreData Model

From Dev

Invalid encoded content of file from iCloud

From Dev

iOS7 / Mavericks apps icloud coredata slow first sync

From Dev

Full CoreData uploading (backup) to iCloud Drive and restore if need

From Dev

Do I need any iCloud settings before using Ensembles for CoreData & iCloud syncing?

From Dev

Tensorflow model always produces mean

From Dev

Chat, conversation, messaging - CoreData model

From Dev

Chat, conversation, messaging - CoreData model

From Dev

Finding the entities of a CoreData model at runtime

From Dev

Model.State is invalid due to MultiSelectList not being able to Convert IEnumerable<SelectListItem> to string

From Dev

MVC5 Application: Create Identity User results in Invalid Model State?

Related Related

  1. 1

    Coredata, NSOrderedSet and iCloud

  2. 2

    Issue with CoreData and photos in iCloud

  3. 3

    CoreData app not syncing with iCloud

  4. 4

    Save CoreData to iCloud (Swift)

  5. 5

    Issue with CoreData and photos in iCloud

  6. 6

    iCloud sync fails with "CoreData: Ubiquity: Invalid option: the value for NSPersistentStoreUbiquitousContentNameKey should not contain periods"

  7. 7

    How to save changed state of dynamic created UISwitch in CoreData model in Swift?

  8. 8

    How to save changed state of dynamic created UISwitch in CoreData model in Swift?

  9. 9

    Model state invalid when using custom RegexAttribute

  10. 10

    Duplicating CoreData records with iCloud sync

  11. 11

    iOS 9 CoreData / ICloud - No such document at URL

  12. 12

    How to get started with SQLite, iCloud and CoreData?

  13. 13

    Trouble with iOS 8 CoreData iCloud sync consistency

  14. 14

    iOS 9 CoreData / ICloud - No such document at URL

  15. 15

    File uploading causes invalid model state in asp.net mvc

  16. 16

    File uploading causes invalid model state in asp.net mvc

  17. 17

    Select Box Not Populated On View After Model State Is Invalid

  18. 18

    iCloud & CoreData: Notification when changed to iCloud Store (First Launch with existing iCloud Data)

  19. 19

    Putting Logic in a CoreData Model

  20. 20

    Invalid encoded content of file from iCloud

  21. 21

    iOS7 / Mavericks apps icloud coredata slow first sync

  22. 22

    Full CoreData uploading (backup) to iCloud Drive and restore if need

  23. 23

    Do I need any iCloud settings before using Ensembles for CoreData & iCloud syncing?

  24. 24

    Tensorflow model always produces mean

  25. 25

    Chat, conversation, messaging - CoreData model

  26. 26

    Chat, conversation, messaging - CoreData model

  27. 27

    Finding the entities of a CoreData model at runtime

  28. 28

    Model.State is invalid due to MultiSelectList not being able to Convert IEnumerable<SelectListItem> to string

  29. 29

    MVC5 Application: Create Identity User results in Invalid Model State?

HotTag

Archive