iOS Core 数据关系故障

Posted

技术标签:

【中文标题】iOS Core 数据关系故障【英文标题】:iOS Core data relationship faults 【发布时间】:2012-10-07 14:33:22 【问题描述】:

我有一个包含 3 个实体的核心数据模型。驱动程序、清单和工作。

每个 Manifest 有一个驱动程序,每个驱动程序有多个 Manifest,每个 Manifest 可以有一个或多个 Job,每个 Job 引用一个 Manifest。

当我像这样构建对象时

//Loop through all the Manifests for the driver
for (SDZManifest *manifest in allData)

    //Create an new instance of manifest in core data
    Manifest *newManifest = (Manifest*)[[SKCoreDataManager sharedInstance] insertObjectforEntity:kEntity_Manifest];

    // ***
    // Set the data for this manifest
    // ***
    [newManifest setDriverID:[NSNumber numberWithInt:[manifest DriverId]]];
    [newManifest setManifestID:[manifest ManifestId]];
    [newManifest setManifestRef:[manifest ManifestRef]];
    [newManifest setSupplierID:[NSNumber numberWithInt:[manifest SupplierId]]];
    [newManifest setTruckID:[NSNumber numberWithInt:[manifest TruckId]]];

    //Get all the jobs for the manifest
    NSArray *allJobsForManifest = [manifest Jobs];
    NSMutableArray *formattedJobsForManifest = [NSMutableArray array];

    //Loop through all the Jobs for this manifiest
    for (SDZJob *job in allJobsForManifest)
    
        //Set the returned data into a Job object
        Job *newJob = (Job*)[[SKCoreDataManager sharedInstance] insertObjectforEntity:kEntity_Job];

        [newJob setInstructions:[job Instructions]];

        [newJob setDateCreated:[job DateCreated]];

        [newJob setCreatedBy:[job CreatedBy]];

        [newJob setIsLive:[NSNumber numberWithBool:[job IsLive]]];

        [newJob setCollectionSequence:[NSNumber numberWithInt:[job CollectionSequence]]];

        [newJob setPlannedDeliveryDate:[job PlannedDeliveryDate]];
        [newJob setPlannedCollectionDate:[job PlannedCollectionDate]];

        [newJob setCustomerRef:[job CustomerRef]];
        [newJob setCustomerName:[job CustomerName]];

        // ***
        // Collection address
        // ***

        //Break down the address
        SDZAddress *collectionAddress = [job CollectionAddress];
        [newJob setCollectionAddressID:[NSNumber numberWithInt:[collectionAddress Id]]];
        [newJob setCollectionAddressLine1:[collectionAddress line1]];
        [newJob setCollectionAddressLine2:[collectionAddress line2]];
        [newJob setCollectionAddressLine3:[collectionAddress line3]];
        [newJob setCollectionAddressCity:[collectionAddress city]];
        [newJob setCollectionAddressCounty:[collectionAddress county]];
        [newJob setCollectionAddressCountry:[collectionAddress country]];
        [newJob setCollectionAddressPostcode:[collectionAddress postcode]];

        //Get the lat and lng of the collection address
        SDZGeoLocation *collectionAddressLatLng = [collectionAddress Geocode];
        [newJob setCollectionAddressLat:[collectionAddressLatLng Lat]];
        [newJob setCollectionAddressLng:[collectionAddressLatLng Lng]];

        // ***
        // Delivery address
        // ***

        //Break down the address
        SDZAddress *deliveryAddress = [job DeliveryAddress];
        [newJob setDeliveryAddressID:[NSNumber numberWithInt:[deliveryAddress Id]]];
        [newJob setDeliveryAddressLine1:[deliveryAddress line1]];
        [newJob setDeliveryAddressLine2:[deliveryAddress line2]];
        [newJob setDeliveryAddressLine3:[deliveryAddress line3]];
        [newJob setDeliveryAddressCity:[deliveryAddress city]];
        [newJob setDeliveryAddressCounty:[deliveryAddress county]];
        [newJob setDeliveryAddressCountry:[deliveryAddress country]];
        [newJob setDeliveryAddressPostcode:[deliveryAddress postcode]];

        //Get the lat and lng of the collection address
        SDZGeoLocation *deliveryAddressLatLng = [deliveryAddress Geocode];
        [newJob setDeliveryAddressLat:[deliveryAddressLatLng Lat]];
        [newJob setDeliveryAddressLng:[deliveryAddressLatLng Lng]];

        [formattedJobsForManifest addObject:newJob];

        NSLog(@"\n\n-- NEW JOB --\n%@\n\n", newJob);
    
    //Show all Jobs for this manifest
    NSLog(@"\n\n-- JOBS FOR MANIFEST --\n%@\n\n", formattedJobsForManifest);


然后我将该 Manifest 对象保存到核心数据中。

当他们单击表格视图单元格时,我从清单数组中获取对象并将其传递给另一个视图。当我记录传递的清单时,它会记录:

-- PASSED MANIFEST --
<Manifest: 0xe59d540> (entity: Manifest; id: 0xe59c3e0 <x-coredata://9F572794-745F-4E43-B4D0-9EC3506EA6E4/Manifest/p5> ; data: 
    driver = nil;
    driverID = 1;
    jobs = "<relationship fault: 0x7b3d290 'jobs'>";
    manifestID = "f705c777-9455-4792-bd84-2deada410dab";
    manifestRef = 001;
    supplierID = 2;
    truckID = 8;
)

当我登录NSLog(@"\n\n-- PASSED MANIFEST JOBS --\n%@\n\n", [passedManifest jobs]); 时,结果是

-- PASSED MANIFEST JOBS --
Relationship 'jobs' fault on managed object (0xe59d540) <Manifest: 0xe59d540> (entity: Manifest; id: 0xe59c3e0 <x-coredata://9F572794-745F-4E43-B4D0-9EC3506EA6E4/Manifest/p5> ; data: 
    driver = nil;
    driverID = 1;
    jobs = "<relationship fault: 0x7b3d290 'jobs'>";
    manifestID = "f705c777-9455-4792-bd84-2deada410dab";
    manifestRef = 001;
    supplierID = 2;
    truckID = 8;
)

为什么它说 托管对象上的关系“作业”错误 (0xe59d540)

当我 NSLog(@"\n\n-- JOB COUNT --\n%u\n\n", [[passedManifest jobs] count]); 它返回 0

【问题讨论】:

这里有类似的问题,可能会有所帮助:***.com/questions/12706103/… @kamleshwar 我在解读你的句子时遇到了麻烦。你能改写吗? 我可能遗漏了一些东西,我看不到您将作业添加到清单实体的位置 Coredata Error "data: <fault>"的可能重复 【参考方案1】:

我刚刚遇到了完全相同的问题。我想我仍然不完全确定为什么,但似乎直到比您预期的更晚才真正获取关系。不过,预取它们实际上非常简单。只需将以下内容添加到您的核心数据请求中...

[request setRelationshipKeyPathsForPrefetching:@[ @"relationship_name" ]];

您可以通过这种方式预加载多个关系,从而预加载数组。

【讨论】:

这救了我。非常感谢!! 很高兴在将近五年后提供帮助 :) 但是请注意,这可能会对性能产生影响,因此只有在您绝对需要预取这些文件而不是延迟加载时才使用它。 这在合并来自私有上下文的数据后对我有帮助。在对主队列上下文进行获取后,我没有得到更新的关系。使用您的代码是诀窍并最终加载了在私有上下文中添加的那些关系。【参考方案2】:

核心数据故障仅表示尚未从“磁盘”加载数据。

不过,它会在需要时自动为您加载,因此无需担心。

【讨论】:

好的,但是为什么作业计数为 0 而应该至少有一个呢?我如何让 XCode 加载它,因为我需要它来显示它。 如果 count 返回 0,那是因为没有作业。您需要仔细查看您的数据...

以上是关于iOS Core 数据关系故障的主要内容,如果未能解决你的问题,请参考以下文章

IOS - 如何在 Core Data Swift 中将数据插入到具有关系的不同表中

遍历 iOS Core Data 关系

iOS Core Data:为 Rails 多态关联设置关系的方法

IOS/objective-c/core-data:如何从相关实体获取属性

在 IOS6 中使用 Core Data(删除/编辑)

iOS Core Data - 将相关数据添加到实体