2012-10-07 53 views
2

我有一個有3個實體的核心數據模型。司機,艙單和工作。iOS核心數據關係錯誤

每個清單有一個驅動程序,每個驅動程序有多個清單,每個清單可以有一個或多個作業,每個作業引用一個清單。

當我建立的對象,像這樣

//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); 

} 

我那麼清單對象保存到核心數據。

當他們點擊表格視圖單元格時,我從清單數組中獲取對象並將其傳遞給另一個視圖。當我登錄傳遞的清單是loggs:

-- 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

這裏是類似的問題,可能會有幫助:http://stackoverflow.com/questions/12706103/core-data-fetch-result-from-multiple-entities-or-relationship – kamleshwar

+0

@kamleshwar我是無法破譯你的句子。你可以重新補充嗎? –

+0

我可能錯過了一些東西,我看不到你在哪裏添加工作來清單實體 – Leonardo

回答

5

核心數據故障只是意味着數據沒有被從「磁盤」裝載尚未。

但是,如果需要,它會自動加載,所以沒有什麼可關注的。

+0

好的,但是爲什麼當它至少有一個時,作業計數爲0?我如何讓XCode加載它,因爲我需要它在我想要顯示的點上。 –

+0

如果count返回0,那是因爲沒有作業。你需要仔細看看你的數據...... – lnafziger

10

我剛剛有完全相同的問題。我想我仍然不完全確定爲什麼,但似乎這種關係實際上並沒有在你期待的時間之前被提取出來。然而,預取它們實際上很簡單。只需添加以下到您的核心數據的請求......

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

您可以預載多重關係這種方式,因此陣列。

+0

這節省了我。非常感謝你!! –

+0

很高興幫助差不多五年之後:)但請注意,這可能會帶來性能方面的影響,所以如果您絕對需要使用這些預取而非延遲加載,請使用此選項。 – shortstuffsushi

+0

這可以幫助我合併來自私人環境的數據。在主隊列上下文執行提取後,我沒有得到更新的關係。使用你的代碼是什麼竅門,並最終加載了私人環境中添加的那些關係。 –