2011-10-27 76 views
2

我在我的核心数据模型中有一对多的关系。我需要创建一个新实体并保存它。该实体具有产生以下代码的一对多关系:iOS核心数据如何正确初始化实体关系?

- (void)addRelationshipEvent1:(NSSet *)values; 
- (void)removeRelationshipEvent1:(NSSet *)values; 

 NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
     NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity]; 
     ApplicationRecord *newManagedObject = (ApplicationRecord*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context]; 


     newManagedObject.startDate = [NSDate date]; 
     newManagedObject.stopDate = [[NSDate date] dateByAddingTimeInterval:120]; 

//不断增加个人的动态性能

是正确的设置-toMany关系设置为零开始?或者我需要初始化一个(空的?)在这里设置并分配它?如果我将初始设置设置为零,我能否稍后添加额外的对象?

 newManagedObject.relationshipEvent1 = nil; 
     newManagedObject.relationshipEvent2 = nil; 
    //... 

     // Save the context. 
     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(); 

     } 

回答

3

亚历克斯,

你不需要并初始化你的人际关系。只需使用提供的访问器或辅助函数,Core Data就可以处理它。 IOW,只在需要实际使用时才担心财产/关系。

Andrew

+0

感谢您的快速回复!现在我需要找出何时保存关系指向的数据http://stackoverflow.com/questions/7921713/ios5-core-data-does-adding-a-set-of-objects-for-一个关系-插入-这些-OBJ –