3

Need some advice when it comes to merge policies of Core Data...Sometimes I get this error NSMergeConflict for NSManagedObject with ObectID... . I tried to search online and found out that I need to remove my FatalError method in appDelegate and have use a merge policy to fix such issues....I read the docs by apple and need some confirmation. My current flow is that, I call an API, whatever result I get back, i save them to CoreData, and then fetch them and display them on my tableView...These results are saved to core data first as they will be displayed when there is no internet. When there is internet, I delete all the previous Objects and repeat my procedure for displaying. Sometimes I get FatalError leading to crash. Which merge policy do i use?

  1. mergeByPropertyObjectTrump
  2. mergeByPropertyStoreTrump
  3. overwrite
  4. rollback

The error I got : Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=( "NSMergeConflict (0x115c27ed0) for NSManagedObject (0x109f129b0) with objectID '0xd000000000440000 <x-coredata://4D2070B8-C99C-49F5-A3B5-7E8E360ECA3A/CDPortfolio/p17>' with oldVersion = 1 and newVersion = <deleted> and old object snapshot = {\n currentValue = \"49141.4\";\n folioNo = 12345;\n growthPercentage = \"5109904.532582804\";\n investmentAmount = 5000;\n isExpanded = 0;\n minRedemptionAmount = 500;\n noOfUnits = 100;\n schemeCategory = 54;\n schemeHouse = 97;\n schemeMasterID = 1000390;\n schemeNAV = \"491.414\";\n schemeName = \"HDFC Taxsaver (G)\";\n schemeReturn = \"1:4.1782,2:17.2281,3:10.9636,5:17.9405,7:11.8873,10:13.5671\";\n}", "NSMergeConflict (0x109f1a340) for NSManagedObject (0x109f128e0) with objectID '0xd000000000400000 <x-coredata://4D2070B8-C99C-49F5-A3B5-7E8E360ECA3A/CDPortfolio/p16>' with oldVersion = 1 and newVers

Sanket Ray
  • 1,071
  • 1
  • 15
  • 24

1 Answers1

7

Merge policy is evil; it is asking core data to delete some of your data. It is MUCH better to avoid a write conflict in the first place by only writing to core data in a SINGLE synchronous way. There is a full explanation on how to set this up here: https://stackoverflow.com/a/42745378/1143046

Jon Rose
  • 8,373
  • 1
  • 30
  • 36