gpt4 book ai didi

iphone - 优化此核心数据请求

转载 作者:行者123 更新时间:2023-12-01 19:12:04 26 4
gpt4 key购买 nike

我在Core Data中有一个名为MusicInterest的实体。我必须一次添加5000个左右,而我当前的过程是查询以查看MusicInterest是否已经存在,如果不创建一个新的。

看来这需要五千次前往商店,以查看每个标题是否存在。当然,也有插入行程,但是5000个查询使我很慢。

每个FacebookFriend都会有多种音乐兴趣,我使用一系列字符串标题枚举每个音乐 friend ,并调用以下代码。

任何想法如何优化呢?

+ (MusicInterest*) musicInterestForFacebookFriend:(FacebookFriend*)facebookFriend WithTitle:(NSString*)musicTitle UsingManagedObjectContext:(NSManagedObjectContext*)moc
{
// query to see if there
NSArray *matches = [self queryForMusicTitle:musicTitle moc:moc];

if (([matches count] >= 1)) {
// NSLog(@"Music already in database");
MusicInterest *existingMusic = [matches lastObject];
[existingMusic addLikedByObject:facebookFriend];
return [matches lastObject];
} else {
// create new Music Interest
MusicInterest *newMusic = [NSEntityDescription insertNewObjectForEntityForName:@"MusicInterest" inManagedObjectContext:moc];
newMusic.title = musicTitle;
[newMusic addLikedByObject:facebookFriend];
return newMusic;
}
}

+ (NSArray *)queryForMusicTitle:(NSString *)MusicTitle moc:(NSManagedObjectContext *)moc
{
// query to see if there
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MusicInterest"];
request.predicate = [NSPredicate predicateWithFormat:@"title == %@", [NSString stringWithFormat:@"%@", MusicTitle]];

NSError *error = nil;
NSArray *matches = [moc executeFetchRequest:request error:&error];
if (error) {
NSLog(@"Error querying title in Music interest. Error = %@", error);
}
return matches;
}

更新:

我采用了Core Data编程指南中建议的设计,并将我的时间从12秒减少到4秒(仍然需要在其他方面进行一些优化:)

该指南仅包含示例代码的一半-我想我将分享完整的实现:
musicArray = [[music componentsSeparatedByString:@", "] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if (obj1 > obj2)
return NSOrderedDescending;
else if (obj1 < obj2)
return NSOrderedAscending;
return NSOrderedSame;
}];

if (musicArray) {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MusicInterest"];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"title IN %@", musicArray]];
[fetchRequest setSortDescriptors:
@[[[NSSortDescriptor alloc] initWithKey: @"title" ascending:YES]]];

NSError *fetchError = nil;
NSArray *musicInterestMatchingTitles = [backgroundContext executeFetchRequest:fetchRequest error:&fetchError];

if ([musicArray count] > 0) {
// walk musicArray and musicInterestsMatchingTitles in parallel
for (int i = 0; i < [musicArray count]; i++) {
NSString *title = musicArray[i];
if (i < [musicInterestMatchingTitles count]) {
MusicInterest *comparingMusicInterest = musicInterestMatchingTitles[i];
// compare each title
if (![title isEqualToString:comparingMusicInterest.title]) {
// if it doesn't exist as a ManagedObject (a MusicInterest), create one
MusicInterest *musicInterest = [MusicInterest createNewMusicInterestUsingManagedObjectContext:backgroundContext];
musicInterest.title = title;
[musicInterest addLikedByObject:friend];
} else {
// otherwise, just establish the relationship
[comparingMusicInterest addLikedByObject:friend];
}
} else {
// if there are no existing matching managedObjects, create one
MusicInterest *musicInterest = [MusicInterest createNewMusicInterestUsingManagedObjectContext:backgroundContext];
musicInterest.title = title;
[musicInterest addLikedByObject:friend];
}
}
}
}

}];
[self saveBackgroundContext:backgroundContext];

最佳答案

“核心数据编程指南”中的Implementing Find-or-Create Efficiently描述了一种可能在此处有用的模式。基本思想是:

  • 通过一些也要存储在其中的唯一ID对要插入/更新的项目列表进行排序
    数据库。
  • 执行一个获取请求,该请求从数据库中获取列表中具有ID(按相同ID排序)的所有对象。
  • 现在并行遍历您的列表和获取的项目数组,以查找必须插入的项目以及已经存在的项目并可以对其进行更新。
  • 关于iphone - 优化此核心数据请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15902749/

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