gpt4 book ai didi

iphone - 核心数据 : does a fetch have to make a trip to persistent store?

转载 作者:行者123 更新时间:2023-11-29 11:05:28 25 4
gpt4 key购买 nike

假设我这样做:

NSManagedObjectContext *context = #a managed object context";
NSString *entityName = #an entity name#;
NSFetchRequest *requestForAll = [NSFetchRequest requestWithEntityName:entityName];
NSArray *allObj = [context executeFetchRequest:requestForAll];

for (NSString *name in allNamesArray){
NSFetchRequest *requestForOne = [NSFetchRequest requestWithEntityName:entityName];
requestForOne.predicate = [NSPredicate predicateWithFormat:@"name == %@",name];
NSArray *ObjsWithName = [context executeFetchRequest:requestForOne];
#do some work with the obj#
}

循环中的获取是否每次都会导致访问持久存储?或者这些提取只会在 coredata 的行缓存中执行?

编辑我写了一段测试代码:您需要创建一个名为“Person”的核心数据实体,它应该有一个名为“name”的属性,该属性是字符串类型。

使用此代码填充一些数据:

self.array = @[@"alkjsdfkllaksjdf",@"asldjflkajdklsfjlk;aj",@"aflakjsdl;kfjalksdjfklajkldhkl;aj",@"aljdfkljalksdjfl;j" ,@"flajdl;kfjaklsdjflk;j",@"akldsjfklajdslkf",@"alkdjfkljaklsdjflkaj",@"alsdjflkajsdflj",@"adlkfjlkajsdfkljkla",@"alkdjfklajslkdfj"];

NSString *firstRunKey = @"oh its first run!";
NSString *firstRun = [[NSUserDefaults standardUserDefaults] objectForKey:firstRunKey];
if (!firstRun) {
for (NSString *name in self.array) {
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
p.name = name;
}
}
[self.managedObjectContext save];
[[NSUserDefaults standardUserDefaults] setObject:firstRunKey forKey:firstRunKey];
[[NSUserDefaults standardUserDefaults] synchronize];

剖析这两种方法,您会发现使用 CoreData 比使用 FilterArray 花费更多的时间!

static int caseCount = 1000;
-(void)usingCoreData
{
NSLog(@"core data");
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
NSArray *allPersons = [self.managedObjectContext executeFetchRequest:request error:nil];

for (int i = 0; i < caseCount; i++){
for (NSString *name in self.array) {
request.predicate = [NSPredicate predicateWithFormat:@"name == %@",name];
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:nil];
}
}
}

-(void)usingFilterArray
{
NSLog(@"filter array");
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
NSArray *allPersons = [self.managedObjectContext executeFetchRequest:request error:nil];

for (int i = 0; i < caseCount; i++){
for (NSString *name in self.array) {
NSArray *array = [allPersons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name == %@",name]];
}
}
}

最佳答案

我想我需要自己回答我的问题。

我测试了一下,每次执行fetch的时候,core data都会把你的NSFetchRequest翻译成SQL命令,然后调用数据库查询,查询结果首先是NSManagedObjectIDs,通过缓存从NSManagedObjectID中获取NSManagedObject。

总之,它缓存对象,但不缓存查询结果。

这意味着您执行同一个 NSFetchRequest 10 次,它会查询您的持久存储 10 次,尽管您会得到 10 次相同的结果。所以在这种情况下,在内存中过滤数组会比获取更好。

关于iphone - 核心数据 : does a fetch have to make a trip to persistent store?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13833529/

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