2013-05-07 58 views
1

我有一个NSPersistentDocument与给定的核心数据模型等如何从NSPersistentDocument中的核心数据预加载数据?

我有一个文件,这个文件创建的,比方说,它的preload.xml。它“包含”几个NSManagedObject s。

我想在我的所有新文档中加载这些对象,这样当我创建一个新文档时,新文档会自动“拥有”preload.xml中的“生存”对象。到目前为止,这里是我做过什么:

  • 我在我的项目复制preload.xml

  • initWithType:error:法(创建一个新文档时调用的方法),有下面的代码:

    NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                  pathForResource:@"preload" 
                  ofType:@"xml"]]; 
    NSError* err = nil; 
    
    [self readFromURL:preloadURL 
          ofType:@"xml" 
          error:&err] ; 
    

这不工作,因为,当我尝试之后救我文件,比方说myNewDoc.xml,这个文件是空的,但我所有的新数据保存到preload.xml

我想知道是否需要创建新的storecontextstoreCoordinator或其他东西。我从来没有处理过这种物体,因为我总是使用NSPersistentDocument

+0

你覆盖'readFromURL克隆技术的帮助:ofType :错误:'?如果是这样,你的版本是什么样子? – 2013-05-07 19:43:44

+0

是的,我重写'readFromURL',但它不起作用。我做了其他的事情,因为:创建一个新的'store'(与我的preload.xml关联),一个新的'storeCoord'和一个新的'MOC'。然后,我在两个MOC之间克隆了我的对象,这并不是很好......但它似乎工作。 – Colas 2013-05-07 20:00:59

+0

好吧,它听起来像原来的问题是与你的'readFromURL:ofType:error:'代码有关的,因为你没有包括它很难说出什么可能是错的。 – 2013-05-07 20:03:26

回答

1

获取已保存的对象:

NSPersistentStoreCoordinator * newPersStoreCoord ; 
    newPersStoreCoord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:theDoc.managedObjectModel] ; 

    NSString * path = [[NSBundle mainBundle] pathForResource:@"preload" 
                 ofType:@"xml"]; 
    NSURL *preloadURL = [NSURL fileURLWithPath:path]; 



    [newPersStoreCoord addPersistentStoreWithType:NSBinaryStoreType 
            configuration:nil 
               URL:preloadURL 
              options:nil 
              error:NULL] ; 

    NSManagedObjectContext * auxMOC = [[NSManagedObjectContext alloc] init] ; 
    [auxMOC setPersistentStoreCoordinator:newPersStoreCoord] ; 

将它们复制到 “活” MOC

[self loadPreloadedDataFromTheMOC:auxMOC 
          toTheMOC:theDoc.managedObjectContext 
          forTheDoc:theDoc] ; 

- (NSManagedObject *)cloneInContext:(NSManagedObjectContext *)context 
        withCopiedCache:(NSMutableDictionary *)alreadyCopied 
        exludeEntities:(NSArray *)namesOfEntitiesToExclude 
        excludeAttributes:(NSArray *)namesOfAttributesToExclude 
{ 
    NSString *entityName = [[self entity] name]; 

    if ([namesOfEntitiesToExclude containsObject:entityName]) 
    { 
     return nil; 
    } 

    NSManagedObject *cloned = [alreadyCopied objectForKey:[self objectID]]; 
    if (cloned != nil) 
    { 
     return cloned; 
    } 


    //create new object in data store 
    cloned = [NSEntityDescription insertNewObjectForEntityForName:entityName 
              inManagedObjectContext:context]; 
    [alreadyCopied setObject:cloned 
         forKey:[self objectID]]; 

    //loop through all attributes and assign then to the clone 
    NSDictionary *attributes = [[NSEntityDescription entityForName:entityName 
              inManagedObjectContext:context] attributesByName]; 


    for (NSString *attr in attributes) 
    { 
     if (![namesOfAttributesToExclude containsObject:attr]) 
     { 
      [cloned setValue:[self valueForKey:attr] forKey:attr]; 
     } 
    } 

    //Loop through all relationships, and clone them. 
    NSDictionary *relationships = [[NSEntityDescription entityForName:entityName 
               inManagedObjectContext:context] relationshipsByName]; 



    for (NSString *relName in [relationships allKeys]) 
    { 
     NSRelationshipDescription *rel = [relationships objectForKey:relName]; 

     NSString *keyName = rel.name; 

     if ([rel isToMany]) 
     { 
      id sourceSet ; 
      id clonedSet ; 

      /* 
      On gère selon que la relation est ordonnée ou non 
      */ 
      if (![rel isOrdered]) 
      { 
       //get a set of all objects in the relationship 
       sourceSet = [self mutableSetValueForKey:keyName]; 
       clonedSet = [cloned mutableSetValueForKey:keyName]; 
      } 
      else 
      { 
       sourceSet = [self mutableOrderedSetValueForKey:keyName]; 
       clonedSet = [cloned mutableOrderedSetValueForKey:keyName]; 
      } 

      NSEnumerator *e = [sourceSet objectEnumerator]; 
      NSManagedObject *relatedObject; 

      while (relatedObject = [e nextObject]) 
      { 
       //Clone it, and add clone to set 
       NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context 
                    withCopiedCache:alreadyCopied 
                     exludeEntities:namesOfEntitiesToExclude 
                    excludeAttributes:namesOfAttributesToExclude]; 
       if (clonedRelatedObject) 
       { 
        [clonedSet addObject:clonedRelatedObject]; 
       } 
      } 
     } 
     else 
     { 
      NSManagedObject *relatedObject = [self valueForKey:keyName]; 
      if (relatedObject != nil) 
      { 
       NSManagedObject *clonedRelatedObject = [relatedObject cloneInContext:context 
                    withCopiedCache:alreadyCopied 
                     exludeEntities:namesOfEntitiesToExclude 
                    excludeAttributes:namesOfAttributesToExclude]; 
       if (clonedRelatedObject) 
       { 
        [cloned setValue:clonedRelatedObject forKey:keyName]; 
       } 
      } 
     } 
    } 

    return cloned; 
}