如何强制coredata重建sqlite数据库模型? [英] How to force coredata to rebuild sqlite database model?

查看:221
本文介绍了如何强制coredata重建sqlite数据库模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有时需要重建和重新填充数据库文件。 SQLite数据库由CoreData堆栈创建和管理。

In my app I sometimes need to rebuild and repopulate database file. SQLite databse is created and managed by CoreData stack.

我想要做的是删除文件,然后简单地重新创建persistentStoreCoordinator对象。

What I'm trying to do is drop the file and then simply recreate persistentStoreCoordinator object.

它在模拟器下工作,但不是在设备上,我得到这样的错误:

It works under simulator but not on device, where I'm getting such an error:

NSFilePath = "/var/mobile/Applications/936C6CC7-423A-46F4-ADC0-7184EAB0CADD/Documents/MYDB.sqlite";
NSUnderlyingException = I/O error for database at /var/mobile/Applications/936C6CC7-423A-46F4-ADC0-7184EAB0CADD/Documents/MYDB.sqlite.  SQLite error code:1, 'table ZXXXX already exists';

我找不到任何原因。它指示两个不同的问题 - Cocoa错误256表示该文件不存在或不可读。但是在创建persistenStoreCoordinator之后创建了文件 IS ,虽然它是空的,但是在执行一些查询后,它消失。

I cannot find the cause of this in any way. It indicates two different problems - Cocoa error 256 indicates that file does not exist or is not readable. But file IS created after creating persistenStoreCoordinator, although it's empty, but after executing some queries it disappears.

第二个消息指示尝试创建alredy现有表格在这种情况下很奇怪。

Second message indicating attempt to create alredy existing table is quite strange in that case.

我很困惑,不能得到这里发生的事情。我的代码看起来像这样:

I'm quite confused and cannot get the point what's going on here. My code looks like this:

NSString *path = [[WLLocalService dataStorePath] relativePath];
NSError *error = nil;

WLLOG(@"About to remove file %@", path);

[[NSFileManager defaultManager] removeItemAtPath: path error: &error];

if (error != nil) {
 WLLOG(@"Error removing the DB: %@", error);
}


[self persistentStoreCoordinator];

WLLOG(@"Rebuild DB result %d", [[NSFileManager defaultManager] fileExistsAtPath: path]);

此代码执行后,DB文件存在,但为空。

After this code is exectued, DB file exists but is empty. When then first query (and all following) is executed, it gives me the error above and file disappears.

有人知道它有什么问题吗?

Does anybody has an idea what's wrong with it?

非常感谢指出正确的方法!

Big thanks for pointing me the right way!

推荐答案

不喜欢你删除它下面的文件。如果你想删除文件,你应该拆掉堆栈,删除文件,然后重建堆栈。这将消除问题。

The Core Data stack does not like you removing the file under it. If you are wanting to delete the file you should tear down the stack, delete the file and then reconstruct the stack. That will eliminate the issue.

部分问题是堆栈保存文件中的数据的缓存。当您删除该文件时,您没有办法清除该缓存,然后您将Core Data置于未知且不稳定的状态。

Part of the problem is that the stack keeps a cache of the data that is in the file. When you remove the file you don't have a way to clear that cache and you are then putting Core Data into an unknown and unstable state.

您可以尝试告诉 NSPersistentStoreCoordinator 您正在删除文件,调用 -removePersistentStore:error:,然后添加新存储与呼叫到 -addPersistentStoreWithType:configuration:URL:options:error:。我现在在ZSync这样做,它的工作很好。

You can try telling the NSPersistentStoreCoordinator you are removing the file with a call to -removePersistentStore:error: and then adding the new store with a call to -addPersistentStoreWithType:configuration:URL:options:error:. I am doing that currently in ZSync and it works just fine.

这篇关于如何强制coredata重建sqlite数据库模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆