需要帮助诊断 NSMergeConflict 错误

Posted

技术标签:

【中文标题】需要帮助诊断 NSMergeConflict 错误【英文标题】:Need Help Diagnosing NSMergeConflict Error 【发布时间】:2016-04-04 12:56:37 【问题描述】:

我一直在努力解决我正在开发的应用程序中的 NSMergeConflict。我知道我可以更改合并策略,但我担心可能会损坏数据,因为我不知道哪些冲突对象是正确的,哪些是过时的。更复杂的是,合并冲突实际上不在我正在修改和保存的对象中。相反,它似乎来自一些我没有直接修改的相关对象。

关系图如下所示:

Order <<-----> Customer

Order <<-----> Rep

Rep   <<-----> Customer

Order 是我正在修改和保存的对象,Customer 和 Rep 是合并错误所抱怨的对象。

我真的不知道如何调试此问题并跟踪我的数据在哪里不同步。有谁知道获取有关我的代码中正在发生的事情的更多数据的好方法?有没有办法记录所有持久性上下文中的内容并查看它们的分歧点?

更新: 我更改了 Order 和 Rep 之间的关系,这样 Rep 不再拥有 Orders 集合,从而解决了一些问题。不幸的是,该解决方案不适用于 Order 和 Customer 之间的关系,我仍然看到错误。

这是我得到的错误的示例堆栈跟踪:

NSMergeConflict (0x1465972e0) for NSManagedObject (0x1462147a0) with objectID '0xd000000000a00008 <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/Customer/p40>' with oldVersion = 13 and newVersion = 14 and old object snapshot = 
address = "\n    cmpnts =     (\n                \n            \"long_name\" = \"2318 W 5TH AVE\";\n            \"short_name\" = \"2318 W 5TH AVE\";\n            types =             (\n                \"street_address\"\n            );\n        ,\n                \n            \"long_name\" = GARY;\n            \"short_name\" = GARY;\n            types =             (\n                locality\n            );\n        ,\n                \n            \"long_name\" = IN;\n            \"short_name\" = IN;\n            types =             (\n                \"administrative_area_level_1\"\n            );\n        ,\n                \n            \"long_name\" = \"Lake County\";\n            \"short_name\" = \"Lake County\";\n            types =             (\n                \"administrative_area_level_2\"\n            );\n        ,\n                \n            \"long_name\" = \"46404-1331\";\n            \"short_name\" = \"46404-1331\";\n            types =             (\n                \"postal_code\"\n            );\n        \n    );\n    lat = \"41.60221\";\n    lng = \"-87.366285\";\n    loc = \"2318 W 5TH AVE, GARY, IN, 46404-1331\";\n";
buyingGroup = "<null>";
compositeName = "20/20 EYE SPECIALIST (#385468)";
created = "2014-10-29 19:36:43 +0000";
"created_by_id" = 5451215fe4b007ffb46431bf;
"current_due" = 0;
customer = 385468;
customerTotal = "0xd0000000db74000a <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/CustomerTotals/p14045>";
detail = "0xd00000000504000c <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/CustomerDetail/p321>";
"device_address_section_key" = 2;
"device_address_sort_key" = "2318 W 5TH AVE";
"device_city_section_key" = G;
"device_city_sort_key" = GARY;
"device_corp_ytd_gross" = 0;
"device_created_section_key" = "01/01/1980";
"device_customer_section_key" = 3;
"device_deleted" = 0;
"device_exported" = "<null>";
"device_last_view" = "2016-04-12 17:04:51 +0000";
"device_latitude" = "41.60221";
"device_loaded" = "2016-04-12 15:25:07 +0000";
"device_longitude" = "-87.366285";
"device_name_section_key" = 2;
"device_note_date" = "<null>";
"device_note_text" = "<null>";
"device_phone_section_key" = 2;
"device_phone_sort_key" = 2198850116;
"device_rolling_12_credits" = 0;
"device_rolling_12_net" = 0;
"device_rolling_12_rd" = 0;
"device_rolling_12_rx" = 0;
"device_state_section_key" = I;
"device_state_sort_key" = IN;
"device_updated" = "<null>";
"device_ytd_credits" = 0;
"device_ytd_gross" = 0;
"device_ytd_net" = 0;
"device_ytd_rd" = 0;
"device_ytd_rx" = 0;
"device_zip_section_key" = 4;
"device_zip_sort_key" = "46404-1331";
"dw_fax" = "";
"dw_open_date" = "1980-01-01 05:00:00 +0000";
"is_inherited" = 0;
jobsonCensus = "0xd000000001a4000e <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/JobsonCensus/p105>";
"jobson_census_id" = 5494225284aec464730b3aa1;
"last_order_date" = "<null>";
"last_sync" = "2016-04-12 15:58:20 +0000";
name = "20/20 EYE SPECIALIST";
primaryContact = "<null>";
"remote_id" = 545141cbe4b024c7a6033055;
"updated_at" = "2015-09-15 11:48:11 +0000";
"updated_by_id" = "<null>";
 and new cached row = 
    address = "\n    cmpnts =     (\n                \n            \"long_name\" = \"2318 W 5TH AVE\";\n            \"short_name\" = \"2318 W 5TH AVE\";\n            types =             (\n                \"street_address\"\n            );\n        ,\n                \n            \"long_name\" = GARY;\n            \"short_name\" = GARY;\n            types =             (\n                locality\n            );\n        ,\n                \n            \"long_name\" = IN;\n            \"short_name\" = IN;\n            types =             (\n                \"administrative_area_level_1\"\n            );\n        ,\n                \n            \"long_name\" = \"Lake County\";\n            \"short_name\" = \"Lake County\";\n            types =             (\n                \"administrative_area_level_2\"\n            );\n        ,\n                \n            \"long_name\" = \"46404-1331\";\n            \"short_name\" = \"46404-1331\";\n            types =             (\n                \"postal_code\"\n            );\n        \n    );\n    lat = \"41.60221\";\n    lng = \"-87.366285\";\n    loc = \"2318 W 5TH AVE, GARY, IN, 46404-1331\";\n";
    buyingGroup = "<null>";
    compositeName = "20/20 EYE SPECIALIST (#385468)";
    created = "2014-10-29 19:36:43 +0000";
    "created_by_id" = 5451215fe4b007ffb46431bf;
    "current_due" = 0;
    customer = 385468;
    customerTotal = "0xd0000000db74000a <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/CustomerTotals/p14045>";
    detail = "0xd00000000504000c <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/CustomerDetail/p321>";
    "device_address_section_key" = 2;
    "device_address_sort_key" = "2318 W 5TH AVE";
    "device_city_section_key" = G;
    "device_city_sort_key" = GARY;
    "device_corp_ytd_gross" = 0;
    "device_created_section_key" = "01/01/1980";
    "device_customer_section_key" = 3;
    "device_deleted" = 0;
    "device_exported" = "<null>";
    "device_last_view" = "2016-04-12 17:04:51 +0000";
    "device_latitude" = "41.60221";
    "device_loaded" = "2016-04-12 15:25:07 +0000";
    "device_longitude" = "-87.366285";
    "device_name_section_key" = 2;
    "device_note_date" = "<null>";
    "device_note_text" = "<null>";
    "device_phone_section_key" = 2;
    "device_phone_sort_key" = 2198850116;
    "device_rolling_12_credits" = 0;
    "device_rolling_12_net" = 0;
    "device_rolling_12_rd" = 0;
    "device_rolling_12_rx" = 0;
    "device_state_section_key" = I;
    "device_state_sort_key" = IN;
    "device_updated" = "<null>";
    "device_ytd_credits" = 0;
    "device_ytd_gross" = 0;
    "device_ytd_net" = 0;
    "device_ytd_rd" = 0;
    "device_ytd_rx" = 0;
    "device_zip_section_key" = 4;
    "device_zip_sort_key" = "46404-1331";
    "dw_fax" = "";
    "dw_open_date" = "1980-01-01 05:00:00 +0000";
    "is_inherited" = 0;
    jobsonCensus = "0xd000000001a4000e <x-coredata://1F648314-CA44-4DA5-B437-07BFCB96D1E9/JobsonCensus/p105>";
    "jobson_census_id" = 5494225284aec464730b3aa1;
    "last_order_date" = "<null>";
    "last_sync" = "2016-04-12 16:06:52 +0000";
    name = "20/20 EYE SPECIALIST";
    primaryContact = "<null>";
    "remote_id" = 545141cbe4b024c7a6033055;
    "updated_at" = "2015-09-15 11:48:11 +0000";
    "updated_by_id" = "<null>";

【问题讨论】:

为什么投反对票?如果问题有问题,请告诉我,以便我解决。 不知道你为什么投了反对票,但你可以添加一些东西来帮助我们帮助你。你能告诉我们你的合并冲突的描述吗?您的核心数据堆栈设置如何?你是如何编写/编辑你的对象的?您为我们提供的背景越多,我们就越能提供帮助。 不确定这是否有帮助,但您可以尝试在保存之前将客户和代表对象恢复为故障。 【参考方案1】:

在我看来,从CustomerRep 的关系是多余的,除非它与Order 实体完全无关。如果代表仅通过订单链接到客户,您可以像这样获取客户的代表:

Set(arrayLiteral: customer.orders.map  $0.rep )

至于记录更多调试输出,您可以打开 sql 调试,尽管您必须推断 Core Data 正在做什么。 (将-com.apple.CoreData.SQLDebug 1 添加到运行方案中。)

如果更新订单会影响您的用户或销售代表,您可以检查您是否有一些会导致意外后果的删除规则。

【讨论】:

很遗憾,Rep 和 Customer 之间的关系并不是多余的。 那么更新订单应该不会影响它,除非你有一些删除用户或代表的级联删除规则。

以上是关于需要帮助诊断 NSMergeConflict 错误的主要内容,如果未能解决你的问题,请参考以下文章

NSManagedObject 的 NSMergeConflict 与单个 ManagedObjectContext

我需要帮助使用 MS 调试诊断工具来分析 CPU 使用率高的 C# 应用程序

带有“#”的 Clang 诊断标志会导致构建错误

人工智能在病理诊断过程中能提供哪些帮助

什么是调试器,它如何帮助我诊断问题?

Source Generator-添加诊断