使用plist将数据插入核心数据时卡住了

时间:2012-12-06 07:59:52

标签: iphone objective-c core-data plist

我在核心数据中有一个名为“Categories”的实体,它有3个属性。

  
      
  1. PARENT_ID
  2.   
  3. CATEGORY_ID
  4.   
  5. CATEGORY_NAME
  6.   

现在在根词典下的CategoriesFile.plist中我创建了3个名为

的数组
  
      
  1. PARENT_ID
  2.   
  3. CATEGORY_ID
  4.   
  5. CATEGORY_NAME
  6.   

我在这些数组中填充了适当的数据。

现在我编写了以下代码来插入数据

   NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"CategoriesFile.plist"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
        {
            // if not in documents, get property list from main bundle
            plistPath = [[NSBundle mainBundle] pathForResource:@"CategoriesFile" ofType:@"plist"];
        }

        // read property list into memory as an NSData object
        NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
        NSString *errorDesc = nil;
        NSPropertyListFormat format;

        // convert static property list into dictionary object
        NSDictionary *thisGardenDictionary = [[NSDictionary alloc] init];
        NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
        if (!plistDictionary)
        {
            NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        }

        [plistDictionary enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id object, BOOL *stop) {
            NSManagedObject *manObj = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context];

            [manObj setValue:[thisGardenDictionary objectForKey:@"category_id"]  forKey:@"category_id"];
        [manObj setValue:[thisGardenDictionary objectForKey:@"parent_id"] forKey:@"parent_id"];
        [manObj setValue:[thisGardenDictionary objectForKey:@"category_name"] forKey:@"category_name"];


             }];
 NSError *error;
    if(![context save:&error])
    {
        NSLog(@"Whoops, couldn't save %@",[error localizedDescription]);
    }

但是我得到了空白的结果。

有人可以说我错过了吗?

2 个答案:

答案 0 :(得分:0)

设置NSManagedObject的属性 e.g

[manObj setName:@"Name"];

等。 然后保存上下文以存储CoreData数据库中的值。

if (![context save:&error]) {
   NSLog(@"Unresolved error : %@", [error userInfo]);
}

答案 1 :(得分:0)

这应该这样做(我相信你已经证实plist被正确读取):

NSDictionary *allArrays = [NSDictionary dictionaryWithContentsOfFile:plistPath];
for (int i = 0; i < [[allArrays objectForKey:@"parent_id"] count]; i++) {
   NSManagedObject *object = [NSEntityDescription 
    insertNewObjectForEntityForName:@"Categories" 
             inManagedObjectContext:context];
    for (NSString *s in @[@"parent_id", @"category_id", @"category_name"]) {
      [object setValue:[[allArrays objectForKey:s] objectAtIndex:i] forKey:s];
    }
}
// save

但是,请注意,这是组织数据的一种非常尴尬的方式。更好的是一系列字典。在您的设置中,如果所有阵列的元素数量不同,则结果将是灾难性的。