结论
我想问题已经解决了。
看起来这个问题与方法无关,但 XCode 没有在构建之间正确清理项目。
看起来在所有这些测试之后,正在使用的 sqlite 文件仍然是第一个没有索引的文件......
当心 XCode 4.3.2,我除了 Clean 不清理的问题之外什么都没有,或者将文件添加到项目中时没有自动添加到捆绑资源中......
感谢您的不同答案..
Update 3
Since I invite anybody to just try the same steps to see if they get the same results, let me detail what I did:
I start with blank project
I defined a datamodel with one Entity, 3 attributes (2 strings, 1 float)
The first string is indexed
![enter image description here](https://i.stack.imgur.com/FfLei.png)
In did finishLaunchingWithOptions, I am calling:
[self performSelectorInBackground:@selector(populateDB) withObject:nil];
populateDb 的代码如下:
-(void)populateDB{
NSLog(@"start");
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
NSManagedObjectContext *context;
if (coordinator != nil) {
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];
}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"input" ofType:@"txt"];
if (filePath) {
NSString * myText = [[NSString alloc]
initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];
if (myText) {
__block int count = 0;
[myText enumerateLinesUsingBlock:^(NSString * line, BOOL * stop) {
line=[line stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
NSArray *lineComponents=[line componentsSeparatedByString:@" "];
if(lineComponents){
if([lineComponents count]==3){
float f=[[lineComponents objectAtIndex:0] floatValue];
NSNumber *number=[NSNumber numberWithFloat:f];
NSString *string1=[lineComponents objectAtIndex:1];
NSString *string2=[lineComponents objectAtIndex:2];
NSManagedObject *object=[NSEntityDescription insertNewObjectForEntityForName:@"Bigram" inManagedObjectContext:context];
[object setValue:number forKey:@"number"];
[object setValue:string1 forKey:@"string1"];
[object setValue:string2 forKey:@"string2"];
NSError *error;
count++;
if(count>=1000){
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
count=0;
}
}
}
}];
NSLog(@"done importing");
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
}
NSLog(@"end");
}
其他一切都是默认的核心数据代码,没有添加任何内容。
我在模拟器中运行它。
我转到 ~/Library/Application Support/iPhone Simulator/5.1/Applications//Documents
有生成的sqlite文件
我把它复制到我的包中
我注释掉了对 populateDb 的调用
我编辑 persistenceStoreCoordinator 以在第一次运行时将 sqlite 文件从捆绑包复制到文档
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
@synchronized (self)
{
if (__persistentStoreCoordinator != nil)
return __persistentStoreCoordinator;
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"myProject" ofType:@"sqlite"];
NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent: @"myProject.sqlite"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:storePath])
{
if ([[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:storePath error:&error])
NSLog(@"Copied starting data to %@", storePath);
else
NSLog(@"Error copying default DB to %@ (%@)", storePath, error);
}
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
}
我从模拟器中删除了该应用程序,我检查 ~/Library/Application Support/iPhone Simulator/5.1/Applications/ 现已删除
我重建并再次启动
正如预期的那样,sqlite 文件被复制到 ~/Library/Application Support/iPhone Simulator/5.1/Applications//Documents
然而,文件的大小明显小于捆绑包中的大小!
另外,使用像这样的谓词进行简单查询 predicate = [NSPredicate predicateWithFormat:@"string1 == %@", string1];清楚地表明 string1 不再被索引
接下来,我创建了一个新版本的数据模型,进行了无意义的更新,只是为了强制进行轻量级迁移
如果在模拟器上运行,迁移需要几秒钟,数据库大小加倍,并且相同的查询现在只需不到一秒即可返回,而不是几分钟。
这可以解决我的问题,强制迁移,但同样的迁移在 iPad 上需要 3 分钟,并且发生在前台。
这就是我现在所处的位置,对我来说最好的解决方案仍然是防止索引被删除,启动时任何其他导入解决方案都需要太多时间。
如果您需要更多说明,请告诉我...
Update 2
因此,到目前为止,我得到的最好结果是使用具有类似数据模型的快速工具生成的 sqlite 文件来为核心数据数据库播种,但在生成 sqlite 文件时没有设置索引。然后,我将这个 sqlite 文件导入到核心数据应用程序中并设置索引,并允许进行轻量级迁移。对于新 iPad 上的 200 万条记录,此迁移静态图像需要 3 分钟。最终应用程序的记录数应是此数量的 5 倍,因此我们仍在考虑较长的处理时间。
如果我走这条路,新的问题将是:可以在后台执行轻量级迁移吗?
Update
我的问题不是如何创建一个工具来填充核心数据数据库,然后将 sqlite 文件导入到我的应用程序中。
我知道该怎么做,我已经做过无数次了。
但直到现在,我还没有意识到这种方法可能会产生一些副作用:在我的例子中,当以这种方式导入 sqlite 文件时,结果数据库中的索引属性显然会“未索引”。
如果您能够验证任何索引数据在此类传输后仍然被索引,我很想知道您如何继续,或者有效地播种此类数据库的最佳策略是什么。
Original
我有一个大型 CSV 文件(数百万行),包含 4 列、字符串和浮点数。
这是针对 iOS 应用程序的。
我需要在第一次加载应用程序时将其加载到核心数据中。
在数据可用之前,该应用程序几乎无法运行,因此加载时间很重要,因为首次使用的用户显然不希望应用程序在能够运行之前需要 20 分钟的加载时间。
现在,我当前的代码在新 iPad 上需要 20 分钟才能处理 200 万行的 csv 文件。
我使用后台上下文来不锁定 UI,并每 1,000 条记录保存一次上下文
我的第一个想法是在模拟器上生成数据库,然后在首次启动时将其复制/粘贴到文档文件夹中,因为这是播种大型数据库的常见非官方方式。不幸的是,索引似乎无法在这样的传输中幸存下来,尽管数据库在几秒钟后就可用了,但性能很糟糕,因为我的索引丢失了。我已经发布了有关索引的问题,但似乎没有一个好的答案。
所以我正在寻找的是:
- 一种提高核心数据加载数百万条记录性能的方法
- 如果数据库在第一次启动时预加载并移动,这是一种保留索引的方法
- 处理这种情况的最佳实践。我不记得使用过任何需要我在首次使用前等待 x 分钟的应用程序(但也许是《每日报》,那是一次糟糕的体验)。
- 任何让用户在没有意识到的情况下等待的创造性方法:在完成教程时进行后台导入等......
- 不使用核心数据?
- ...