核心数据和10 000个音频文件 [英] Core Data and 10 000 audio files

查看:94
本文介绍了核心数据和10 000个音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体Word,其中有两个属性--inputSound和outputSound,两者都很少,大约有10KB的音频文件,但在我的SQL数据库中这个实体会有4000个实例。

I have an entity "Word", and the are two properties in it - inputSound and outputSound,both of them are little,about 10KB audio files, but there would be 4000 instances od this entity in my SQL database.

首先,我尝试将它们作为二进制数据存储在我的SQL中,这使它工作得很慢。
那么我找到了存储在外部记录文件中的选项,我想问一下,如果这对我有帮助,或者我最好只将url存储到我的实体属性中的音频文件中?

First, i tried to store them as a binary data in my SQL, and it made it work quite slow. So then i found an option to "Store in External Record File" and i want to ask,if this would help me,or i should better just store url to the audio file in my entity property?

如果第一次更好,我应该如何在第一次发布时将我的pesistent商店从Bundle移到文件目录?

If first is better, how should i move my pesistent store from Bundle to documents directory at the first launch?

现在我只是在处理一个SQL(我在我的应用程序的开发人员中预先填充了它),但是我应该如何使用这么多文件呢?并且我能以某种方式在背景中这样做以使我的第一次启动更快且更多用户 - 友好?

Right now i'm just coping an SQL(it's prepopulated by me in the developer's part of my app),but how should i do that with so many files?And can i do that in backgrouns somehow to make my first launch faster and more user-friendly?

我现在就这样做了

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

if (__persistentStoreCoordinator != nil)
        return __persistentStoreCoordinator;

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *defaultStore = [documentsDirectory stringByAppendingPathComponent:@"Easy10.sqlite"];

NSString *sqliteInBundle = [[[NSBundle mainBundle] pathForResource:@"Easy10" ofType:@"sqlite"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Easy10.sqlite"]; 
NSError *error =nil;
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:defaultStore])
    [fileManager copyItemAtPath:sqliteInBundle toPath:defaultStore error:&error];



__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];    
return __persistentStoreCoordinator;

}

推荐答案

对于二进制(尤其是大型的,虽然你的很多的情况也可以适用),推荐的方法似乎是使用Core Data来存储元数据,包括文件路径到实际的二进制本身。因此,您将这些内容存储在Application的沙箱中(即,可能是Documents目录中创建的子目录)。然后你可以使用 [NSData dataWithContentsOfFilePath] 或类似的方法来实际获取你想要播放它们的字节数,但你的核心数据提取并没有因为试图铲除而减慢周围的那些字节。

For binary (and especially large ones, though your case of "many" of them can also apply) the recommended method seems to be to use Core Data to store the metadata, including a file path to the actual binary itself. Thus you'd store these things somewhere in the Application's sandbox (i.e., perhaps a subdirectory of your creation in the "Documents" directory). Then you can use the [NSData dataWithContentsOfFilePath] or similar to actually get the bytes when you want to play them, but your Core Data fetches aren't slowed down by trying to shovel those bytes around.

绝对看看有关优化核心数据的WWDC视频。如何使用Instruments和其他SQL Debug Timing开关的绝佳示例,以确保您正在优化的内容......实际上已经过优化。 :)

Definitely take a look at the WWDC video on optimizing Core Data. Great examples of how to use Instruments and additional "SQL Debug Timing" switches to make sure that what you're optimizing… actually got optimized. :)

这篇关于核心数据和10 000个音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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