需要帮助诊断NSMergeConflict错误

时间:2016-04-04 12:56:38

标签: ios swift core-data runtime-error magicalrecord

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

关系图看起来像这样:

Order <<-----> Customer

Order <<-----> Rep

Rep   <<-----> Customer

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

我真正无法弄清楚的是如何调试此问题并追踪我的数据不同步的位置。有谁知道一个很好的方法来获取有关我的代码中发生的事情的更多数据?有没有办法记录所有持久化上下文中的内容并查看它们的分歧?

更新 我改变了Order和Rep之间的关系,使得Rep不再拥有Orders集合并解决了一些问题。不幸的是,该解决方案不是订单和客户之间关系的选项,我仍然看到错误。

以下是我收到的错误的示例堆栈跟踪:

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 个答案:

答案 0 :(得分:0)

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

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

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

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