gpt4 book ai didi

macos - 如何在 NSPersistentDocument 中预加载 Core Data 中的数据?

转载 作者:行者123 更新时间:2023-12-03 17:12:51 24 4
gpt4 key购买 nike

我有一个带有给定核心数据模型等的NSPersistentDocument

我有一个由该文档创建的文件,假设它是preload.xml。它“包含”几个 NSManagedObject

我想将这些对象加载到我的所有新文档中,以便当我创建新文档时,新文档会自动“拥有”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)时,该文件为空,但所有新数据都保存到 预加载中。 xml.

我想知道是否需要创建一个新的storecontextstoreCoordinator或其他东西。我从未处理过此类对象,因为我一直使用 NSPersistentDocument

最佳答案

获取保存的对象:

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

关于macos - 如何在 NSPersistentDocument 中预加载 Core Data 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16425908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com