我目前正在更新一个应用程序以使用Core Data。你可以说的应用程序是一个"数据库查看器",一次只能查看一个数据库。每个数据库都保存在自己单独的文件夹中。目前,数据被下载并存储为一组plist文件。

在新版本中,我需要将这些plist数据库转换为核心数据存储(每个数据库一个存储。)我已经设置了创建单独的存储文件的方法,并创建实体。问题是所有实体都保存到我创建的第一个数据库中,而不是保存到"当前"或"最后创建"文件中。

我使用的基本过程是:

//For each database {
//Create the sqlite file and set up NSManagedObjectContext
[MagicalRecord setupCoreDataStackWithStoreNamed:
    [NSURL fileURLWithPath:
    [NSString stringWithFormat:@"%@/%@/%@.sqlite",
    dirPath, directory, directory]]];
NSManagedObjectContext *managedObjectContext = 
    [NSManagedObjectContext MR_contextForCurrentThread];

//Iterate through all the plist files and create the necessary entities.
//Save new entities to file
[managedObjectContext MR_save];
//Clean up all cashes
[MagicalRecord cleanUp];
}

如何在商店之间正确切换,基本上在每个交换机之间"重置"所有内容。最好(如果可能的话)使用神奇的记录。

编辑:我已经发现了问题的一部分,并删除了大部分不需要的行为。事实证明,你不能可靠地打电话 [MagicalRecord cleanUp] 在后台线程上。此外,它没有做我认为应该做的事情(见下文)。我最终在每次"保存"之后回调到主线程以重置核心数据堆栈。这样做会为前三个数据库创建一个新的上下文。之后,它从三个数据库之前的数据库中复制上下文。所以在一个循环中使用相同的三个上下文。

这是我目前拥有的;我通过创建后台线程来启动该过程,并运行代码以在后台创建单个数据库:

backgroundQueue = dispatch_queue_create("com.BrandonMcQuilkin.myQueue", NULL);
    dispatch_async(backgroundQueue, ^(void) {
        [self createSQLiteDatabase:updateList];
    });

然后创建堆栈和数据库:

- (void)createSQLiteDatabase:(NSArray *)updateList
{
    NSString *directory = [updateList objectAtIndex:0];
    [MagicalRecord setupCoreDataStackWithStoreNamed:
        [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@/%@.sqlite",
        dirPath, directory, directory]]];
    NSManagedObjectContext *managedObjectContext = 
        [NSManagedObjectContext MR_contextForCurrentThread];
    //Check to see if the stack has reset
    NSLog(@"Before:%i", [[Competition MR_findAllInContext:managedObjectContext] count]);

    //Create and add entities to context...

    //Prepare for next loop
    NSLog(@"After:%i", [[Competition MR_findAllInContext:managedObjectContext] count]);
    [managedObjectContext MR_saveNestedContexts];
    [NSManagedObjectContext MR_resetContextForCurrentThread];

    NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:updateList];
    [temp removeObjectAtIndex:0];

    dispatch_async(dispatch_get_main_queue(), ^(void){
        [self shouldContinueUpdating:temp];
    });

然后重置所有内容并重复所有数据库:

- (void)shouldContinueUpdating:(NSArray *)databases
{
    //preform cleanup on main thread and release background thread
    [MagicalRecord cleanUp];
    dispatch_release(backgroundQueue);

    if ([databases count] != 0) {
        backgroundQueue = dispatch_queue_create("com.BrandonMcQuilkin.myQueue", NULL);
        dispatch_async(backgroundQueue, ^(void) {
            [self createSQLiteDatabase:databases];
        });
    }
}

有了两个Nslog,我在控制台中得到这个:(使用六个数据库,无论我转换多少个数据库,模式都是相同的。)

//First Loop
Before:0
After:308
//Second Loop
Before:0
After:257
//Third Loop
Before:0
After:37
//Fourth Loop
Before:308 
After:541 
//Fifth Loop
Before:257
After:490
//Sixth Loop
Before:37
After:270
... Keep adding to each of the three contexts.

[MagicalRecord cleanUp] 没有按它说的做。这是该方法应该做的。

+ (void) cleanUpStack;
{
[NSManagedObjectContext MR_cleanUp];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:nil];
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:nil];
[NSPersistentStore MR_setDefaultPersistentStore:nil];
}

但事实证明,NSStoreCoordinator每次我保存,是相同的协调器,在相同的内存位置,每个商店都在闲逛。有些东西坏了。..

有帮助吗?

解决方案 2

原来我遇到的问题是因为MagicalRecord中的一个错误。我在这里提交了一个git问题: https://github.com/magicalpanda/MagicalRecord/issues/270

其他提示

MagicalRecord可能不是这项工作的最佳工具。..

首先,让我们纠正您对setupCoreDataStackWithStoreNamed的使用:方法。该参数采用NSString,而不是URL,也不是文件路径。MagicalRecord将为您选择正确的路径,并在那里创建您的商店。您的结果sqlite文件可能会以您想要的路径命名。

接下来,您需要为此文件动态创建CoreData模型。这是一种艰难的,但可能的。您需要遍历这些plist文件,并解释实体,属性和关系,并创建相应的NSEntityDescriptions,NSAttributeDescriptions和Nsrelationshipdescriptions并"手动"填充NSManagedObjectModel。你会想找方法的

- [NSManagedObjectModel setEntities:(NSArray *)]

以及NSEntityDescription、NSAttributeDescription和NSRelationshipDescription的创建方法。

您还需要将此模型保存在某个地方,这样您就不必每次都重新创建它。幸运的是,它符合NSCoding,因此您应该能够将其保存到磁盘。

之后,您可能需要填充数据。从这里,MagicalRecord可以帮助你。我建议看看 导入数据变得容易 我写的博客文章 可可是我的女朋友

如果你想"切换存储",我想这意味着你想为每个plist文件创建一个新的存储,那么你将不得不拆除每个文件的整个核心数据堆栈。如果你设法使用MagicalRecord这个项目,你需要看看[MagicalRecord清理],并重新开始。如果每个模型都是相同的,则可以通过释放持久存储协调器并为您的存储创建一个新的。但由于你的"模式"可能会有所不同,你只想从头开始。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top