gpt4 book ai didi

objective-c - 巨大的核心数据对象

转载 作者:可可西里 更新时间:2023-11-01 05:42:19 25 4
gpt4 key购买 nike

我试图将 20 万条消息添加到核心数据对象,以证明 Twitter 应用程序的概念。这很好用,并且添加了我的实体。我有一个 UISearchBar 和一个 UITableView 来显示它们。然而,对于每一个新的 1000 个对象,我的时间似乎呈指数增长。这是正常的吗?我希望 CoreData 能够很好地处理大型数据集。您对处理如此庞大的数据集有更好的建议吗?我想知道 Dictionary apps 是如何工作的。

我的控制台输出在这里:

-2012-03-26 22:19:28.126 TweetReader[3668:707] Done 1000
-2012-03-26 22:19:40.335 TweetReader[3668:707] Done 2000
-2012-03-26 22:19:55.136 TweetReader[3668:707] Done 3000
-2012-03-26 22:20:18.569 TweetReader[3668:707] Done 4000
-2012-03-26 22:20:50.166 TweetReader[3668:707] Done 5000
-2012-03-26 22:21:30.284 TweetReader[3668:707] Done 6000
-2012-03-26 22:22:19.096 TweetReader[3668:707] Done 7000
-2012-03-26 22:23:16.091 TweetReader[3668:707] Done 8000
-2012-03-26 22:24:21.321 TweetReader[3668:707] Done 9000
-2012-03-26 22:25:35.017 TweetReader[3668:707] Done 10000
-2012-03-26 22:26:57.250 TweetReader[3668:707] Done 11000
-2012-03-26 22:28:27.563 TweetReader[3668:707] Done 12000
-2012-03-26 22:30:06.202 TweetReader[3668:707] Done 13000
-2012-03-26 22:31:52.645 TweetReader[3668:707] Done 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];
}
}

最佳答案

嘿,如果你有 200k 条消息,首先要找到适合每条消息长度的最佳存储空间。这将是反复试验,我发现 3000 是最佳的。每 3000 次执行 [保存上下文] 例如:

      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;



}

如果有什么事请告诉我

关于objective-c - 巨大的核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com