我试图将200k消息添加到核心数据对象,以获取twitter应用程序的概念证明。这很好用,我的实体也加入了。我有UISearchBar
和UITableView
来显示这些内容。但是,对于每个新的1000个对象,我的时间似乎呈指数级增长。这是正常的吗?我希望CoreData
能够很好地处理大型数据集。你有更好的建议来处理如此庞大的数据集。我想知道Dictionary apps
如何运作。
我的控制台输出在这里:
-2012-03-26 22:19:28.126 TweetReader [3668:707]完成1000
-2012-03-26 22:19:40.335 TweetReader [3668:707]完成2000
-2012-03-26 22:19:55.136 TweetReader [3668:707]完成3000
-2012-03-26 22:20:18.569 TweetReader [3668:707]完成4000
-2012-03-26 22:20:50.166 TweetReader [3668:707]完成5000
-2012-03-26 22:21:30.284 TweetReader [3668:707]完成6000
-2012-03-26 22:22:19.096 TweetReader [3668:707]完成7000
-2012-03-26 22:23:16.091 TweetReader [3668:707]完成8000
-2012-03-26 22:24:21.321 TweetReader [3668:707]完成9000
-2012-03-26 22:25:35.017 TweetReader [3668:707]完成10000
-2012-03-26 22:26:57.250 TweetReader [3668:707]完成11000
-2012-03-26 22:28:27.563 TweetReader [3668:707]完成12000
-2012-03-26 22:30:06.202 TweetReader [3668:707]完成13000
-2012-03-26 22:31:52.645 TweetReader [3668:707]完成14000
个
这是我保存到CoreData
的代码:
for (NSInteger i = 1; i <= 200000; i++) {
NSAutoreleasePool * myPool = [[NSAutoreleasePool alloc] init];
Tweet *tweetie = [NSEntityDescription insertNewObjectForEntityForName:@"Tweet" inManagedObjectContext:self.managedObjectContext];
tweetie.name = [NSString stringWithFormat:@"%10d",i];
tweetie.message =[NSString stringWithFormat:@"%10d",i];
// Save the context after 1000 objects.
if (! (i % 1000)) {
NSError *error;
NSLog(@"Done %d",i);
if (![managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[myPool release];
}
}
答案 0 :(得分:3)
for(i=0; i<num of objects;i++)
{ // form the object to be saved in context
if(i%3000==0){ // save the num of records , optimum value
NSError *error;
NSLog(@"saved rec nu %d",i);
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
return NO;
}
[context processPendingChanges];// most important thing , if not objects will
// keep adding in context and time would for insertion would drastically increase.
}
//for last set of the objects which are less than 3000
NSError *error;
NSLog(@"saved rec nu %d",i);
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
return NO;
}
如果有的话,请告诉我..
答案 1 :(得分:1)
您在数据库中编制了哪些列?您是否尝试在添加时锁定上下文以查看它是否可以优化此操作(即以事务方式执行插入操作?)
核心数据在批量插入方面非常糟糕。对此的解决方案可能只是直接使用sqlite - 请参阅此文章,了解必须执行此操作的人的真实案例研究:http://inessential.com/2010/02/26/on_switching_away_from_core_data