核心数据,文件大,速度慢

发布于 2024-10-25 07:03:55 字数 1751 浏览 1 评论 0原文

我正在编写一个应用程序,它存储大量非常短的字符串(主要是一到三个 unicode 字符)和大量关系。这会导致以 XML 格式存储关系的巨大开销,以二进制格式存储关系更是如此(这很奇怪)。因此,如果我使用 XML 或二进制,我会得到巨大的文件以及很长的保存和加载时间。

SQLite 格式更紧凑,保存速度更快(特别是在发生小更改的情况下),但由于某种原因,使用格式为“$something BEGINSWITH[c] fieldInMyObject”的谓词的查询不起作用,而且我不能没有它们。

我可以做些什么来减少文件的体积并加快加载和保存的速度(除了直接使用 SQLite 之外)?

此致, 蒂莫菲。

UPD 这是保存数据的代码:

‐ (IBAction) saveAction:(id)sender {

NSError *error = nil;

if (![[self managedObjectContext] commitEditing]) {
    NSLog(@"%@:%s unable to commit editing before saving", [self class], _cmd);
}

if (![[self managedObjectContext] save:&error]) {
    [[NSApplication sharedApplication] presentError:error];
}

}

这是加载数据的代码(用于创建新文件和加载现有文件): <代码>

- (void) panelReturnedURL:(NSURL *)url {
    NSManagedObjectModel *mom = [self managedObjectModel];
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
initWithManagedObjectModel:mom];
    NSError *error = nil;
    if (![persistentStoreCoordinator addPersistentStoreWithType: NSBinaryStoreType
                                                  configuration:nil 
                                                            URL:url 
                                                        options:nil 
                                                          error:&error]) {
        [NSApp presentError:error];
    }
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: persistentStoreCoordinator];
    [mainWinController window];
}

当对象被修改时,我不会不保存,当应用程序退出或用户显式保存它时,上下文会被保存。

I am writing an application, which stores a huge number of very short strings (mostly one to three unicode chars) and lots of relationships. This results in a massive overhead for storing the relationships in the XML format and even more so in the Binary format (which is strange). So if I use XML or Binary, i get huge files and very long save and load times.

The SQLite format is more compact and saves faster (especially in case of small changes), but for some reason the queries using predicates with format "$something BEGINSWITH[c] fieldInMyObject" do not work, and i can't do without them.

Is there anything i can do to reduce the volume of the files and speed up loading and saving (apart from using SQLite directly)?

Best regards,
Timofey.

UPD
Here is the code for saving the data:

‐ (IBAction) saveAction:(id)sender {

NSError *error = nil;

if (![[self managedObjectContext] commitEditing]) {
    NSLog(@"%@:%s unable to commit editing before saving", [self class], _cmd);
}

if (![[self managedObjectContext] save:&error]) {
    [[NSApplication sharedApplication] presentError:error];
}

}

And here is the code for loading data (both for creating new files and loading existing ones):

- (void) panelReturnedURL:(NSURL *)url {
    NSManagedObjectModel *mom = [self managedObjectModel];
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
initWithManagedObjectModel:mom];
    NSError *error = nil;
    if (![persistentStoreCoordinator addPersistentStoreWithType: NSBinaryStoreType
                                                  configuration:nil 
                                                            URL:url 
                                                        options:nil 
                                                          error:&error]) {
        [NSApp presentError:error];
    }
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: persistentStoreCoordinator];
    [mainWinController window];
}

And i don't don't save when objects are modified, the context is saved when the application quits or when the user explicitly saves it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-11-01 07:03:55

对于大文件来说,XML 和二进制文件可能会很慢,因为它们必须以一个块的形式完全读入内存才能工作。如果您有大量数据,则确实需要使用 SQLite 存储。

您的谓词问题与 SQLite 存储无关。这种类型的谓词是常规使用的。我建议发布一个单独的问题,其中包含您的实体的布局和您想要使用的谓词:

XML and binary can be slow for large files because they have to be read entirely into memory in one chunk in order to work. If you have a lot of data your really need to use an SQLite store.

Your problems with the predicate having nothing to do with the SQLite store. That type of predicate is used routinely. I would suggest posting a seperate question with a layout of your entities and the predicate you want to use:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文