CoreData - 访问另一台计算机上的数据库时出现问题
我的 CoreData 有问题,但我确信我在概念上做错了。
我正在尝试从另一台计算机访问网络中一台计算机上的 CoreData sql 文件。我正在尝试从类似集群的应用程序中执行此操作。每台机器都有相同的软件副本,并且需要指向这台机器上的数据库。
我的模型和上下文对于数据库所在的机器加载得很好。另一台机器给我错误 13400 NSPercientStoreInvalidTypeError
这是一些代码:
NSError *error = nil;
NSURL *mdlurl = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"OsiriXDB_DataModel" ofType:@"mom"]];
_model = [[NSManagedObjectModel alloc] initWithContentsOfURL: url];
NSURL *dburl = [NSURL URLWithString:[NSString stringWithUTF8String:_DBPath.c_str()]];
// The dburl has a format like: file://192.168.0.2/Users/slate/Documents/OsiriX%20Data/Database.sql which addresses the machine the data sits on.
_storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: _model];
_context = [[NSManagedObjectContext alloc] init];
[_context setPersistentStoreCoordinator: _storeCoordinator];
if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dburl options:nil error:&error]) {
NSLog(@"Error loading store: %@", error); // Error Shows up Here
NSLog(@"MOM: %@",_model); // Model looks OK. Lots of print outs, with the correct names and stuff. (so technical).
}
我承认对 CoreData 不太了解。是因为它在不同的机器上吗?我读了 这个在线,但我不认为这是我的问题。如果是,我不知道如何修复它,因为我在我的 ~/Library/Application\ Support/
目录中找不到与任何一个相关的任何 .xml
文件MyApp
或 OsiriX
这是创建数据库的程序。
我通过网络加载 CoreData 是否做错了?
如果没有,我应该做什么?
谢谢,
I've got a problem with my CoreData, but I'm sure I'm doing something wrong conceptually.
I'm trying to access a CoreData sql file on one machine in my network from another machine. I'm trying to do this from a cluster-like application. Each machine has the same copy of the software and needs to point to the database on this one machine.
My model and context load fine for the machine that the database is on. The other machine, gives me error 13400 NSPersistentStoreInvalidTypeError
Here's the bit of code:
NSError *error = nil;
NSURL *mdlurl = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"OsiriXDB_DataModel" ofType:@"mom"]];
_model = [[NSManagedObjectModel alloc] initWithContentsOfURL: url];
NSURL *dburl = [NSURL URLWithString:[NSString stringWithUTF8String:_DBPath.c_str()]];
// The dburl has a format like: file://192.168.0.2/Users/slate/Documents/OsiriX%20Data/Database.sql which addresses the machine the data sits on.
_storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: _model];
_context = [[NSManagedObjectContext alloc] init];
[_context setPersistentStoreCoordinator: _storeCoordinator];
if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dburl options:nil error:&error]) {
NSLog(@"Error loading store: %@", error); // Error Shows up Here
NSLog(@"MOM: %@",_model); // Model looks OK. Lots of print outs, with the correct names and stuff. (so technical).
}
I confess to not knowing a ton about CoreData. Is it because it's on a different machine? I read this online but I don't think that's my issue. If it is, I have no idea how to fix it because I can't find any .xml
files in my ~/Library/Application\ Support/
directory relating to either MyApp
or OsiriX
which is the program that created the database.
Am I doing the wrong thing to load CoreData across a network?
If not, what should I be doing?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表明持久存储协调器认为该文件不是 NSSQLiteStoreType 的正确格式。这表明该文件已找到。如果它无法找到该文件或访问该目录,您将收到另一个错误。
我不确定你的问题具体是什么,但我可以告诉你,核心数据并不是一个并发数据库。它甚至根本不是一个真正的数据库。它实际上是一个运行时对象图管理系统,旨在管理应用程序的模型层,并将持久性作为选项添加到侧面。没有用于控制应用程序的多个实例同时访问同一商店的核心数据选项。您也许可以通过将商店设置为只读来做到这一点,但我不确定。
听起来您需要在服务器上运行一个真正的数据库。
The error indicates that persistent store coordinator thinks that the file is not the proper format for a NSSQLiteStoreType. That suggest that the file was found. If it couldn't locate the file or access the directory you would get another error.
I'm not sure what you problem is specifically but I can tell you in general that Core Data is not intended as a concurrent database. It's not even really a database at all. It's actually a runtime object graph management system intended to manage an app's model layer with persistence tacked on the side as an option. There are no Core Data options for controlling multiple instances of an app simultaneously accessing the same store. You might be able to do so by setting the store as readonly but I don't know for sure.
It sounds like you need a real database running on your server.