2015-01-07 33 views
0

我正在構建一個核心數據IOS應用程序,並且爲了測試目的,我創建了一個包含演示數據的PLIST文件。我的核心數據模型包含具有多個關係的多個實體。如何構建一個plist文件來填充核心數據關係模型

就像我說過的,我能夠創建PLIST並在首次啓動時自動加載數據,但是我能夠「預先構建」這些關係。

我的問題是:這可以做到,如果是的話如何?

我只是不明白我怎麼可以設置一鍵另一個對象的價值...

任何幫助將不勝感激。

與此同時,我會試圖用「虛擬索引」來重新創建這些關係,如果我能夠設法完成這項工作的話。

+0

做你的核心數據實體有關係?如果是這樣,你有什麼不明白爲這些關係分配值? –

+0

我的意思是從物業清單的角度來看。它不像一個普通的相關數據庫,您可以將對象ID分配給相關字段,然後離開,在Core Data中,整個對象被分配給屬性,而我不知道如何在屬性中實現該屬性列表... –

回答

0

那麼,「虛擬目錄」的方式時尚

後曾相知對象的位置(索引)是什麼在我的字典的數組,我能夠加載我的實體與後設置所需的關係適當的數據。

BTW,有人試圖完成同樣的事情,這裏是我帶的過程:

  1. 在Xcode中創建(一個新的plist文件文件 - >新建 - >文件 - > IOS資源 - >物業List)
  2. 填充PLIST:
    • 在根下,在我的數據模型中爲每個實體添加了ARRAY項。
    • 在每個ARRAY中,爲每個實體對象添加一個DICTIONARY項目。
    • 在每個DICTIONARY中添加反映屬性名稱,類型和值的項目。
  3. 增加了我的第一個TableViewController的viewDidLoad方法在以下線路:

    if ([[self.fetchedResultsController fetchedObjects] count] < 1) 
    { 
        [Utilities loadDefaultDataInManagedObjectContext:self.managedObjectContext]; 
    } 
    
  4. 我 「loadDefaultDataInManagedObjectContext:(的NSManagedObjectContext *)上下文」 的方法:

    + (void)loadDefaultDataInManagedObjectContext:(NSManagedObjectContext *)context 
    { 
        NSError *errorDesc = nil; 
        NSPropertyListFormat format; 
        NSString *plistPath; 
        NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    
        plistPath = [rootPath stringByAppendingPathComponent:@"DefaultData.plist"]; 
    
        if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
        { 
         plistPath = [[NSBundle mainBundle] pathForResource:@"DefaultData" ofType:@"plist"]; 
        } 
    
        NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
        NSDictionary *entities = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:plistXML options:NSPropertyListMutableContainers format:&format error:&errorDesc]; 
        if (!entities) 
        { 
         NSLog(@"Error reading plist: %@, format: %lu", errorDesc, format); 
        } 
        else 
        { 
         // ENTITY 1 data 
         NSArray *entityNames = [entities valueForKey:@"EntityName"]; 
         for (NSDictionary *entityName in entityNames) 
         { 
          EntityName *newEntity = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context]; 
          newEntity.attributeName1 = [entityName valueForKey:@"attributeName1"]; 
          newEntity.attributeName2 = [entityName valueForKey:@"attributeName2"]; 
         } 
    
         // ENTITY 2 data 
         //... 
    
         // ENTITY 3 data 
         //... 
    
         NSError *error = nil; 
         if (![context save:&error]) 
         { 
          /* 
          Replace this implementation with code to handle the error appropriately. 
    
          abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
          */ 
    
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
          abort(); 
         } 
        } 
        NSLog(@"Loaded Default Data"); 
    
        [Utilities setRelationshipsInManagedObjectContext:context]; 
    } 
    
  5. 我「setRelationshipsInManagedObjectContext:(NSManagedObjectContext *)context」method:

    + (void)setRelationshipsInManagedObjectContext:(NSManagedObjectContext *)context 
    { 
        NSError *error; 
    
        // Initialize all neccessary fetch requests 
        NSFetchRequest *entityName1Request = [[NSFetchRequest alloc] initWithEntityName:@"entityName1"]; 
        entityName1Request.predicate = nil; 
        NSFetchRequest *entityName2Request = [[NSFetchRequest alloc] initWithEntityName:@"entityName2"]; 
        entityName2Request.predicate = nil; 
        NSFetchRequest *entityName3Request = [[NSFetchRequest alloc] initWithEntityName:@"entityName3"]; 
        entityName3Request.predicate = nil; 
        //... 
    
        NSArray *entities1 = [context executeFetchRequest:entityName1Request error:&error]; 
        NSArray *entities2 = [context executeFetchRequest:entityName2Request error:&error]; 
        NSArray *entities3 = [context executeFetchRequest:entityName3Request error:&error]; 
        //... 
    
        // Declaring my Objects 
        Entity1 *entity1; 
        Entity1 *entity2; 
        Entity1 *entity2; 
        // ... 
    
        if (entities1) 
        { 
         for (entity1 in entities1) 
         { 
          // set the required relationship according to your data and schema 
          if ([entity1.attributeName1 isEqualToString:@"Whatever"]) 
          { 
           entity2 = (Entity2 *)[entities2 objectAtIndex:0]; 
           entity1.relatedAttribute = entity2; 
          } 
          else if ([entity1.attributeName1 isEqualToString:@"SomethingElse"]) 
          { 
           entity3 = (Entity3 *)[Entities3 objectAtIndex:2]; 
           [entity1 addEntity3Object:entity3]; // for to many relationships 
          } 
          else // ... 
          { 
          } 
         } 
        } 
    
        // repeat the same process for your other entities that contain relationship 
    
        if (![context save:&error]) 
        { 
         /* 
         Replace this implementation with code to handle the error appropriately. 
    
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
         */ 
         NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
         abort(); 
        } 
        NSLog(@"Set relationships"); 
    } 
    

仍然希望有人確認或否認在PLIST中做所有這些的可能性。

TIA,

米歇爾

+0

如果您從屬性列表開始,則需要類似您的解決方案。在屬性列表中沒有任何關係建模,因爲它們不是爲此目的而設計的。 –

+0

謝謝。我對此的想法越多,我得出的結論就越多:對象需要立即執行,然後才能與其他對象進行關聯...... –