gpt4 book ai didi

iphone - 核心数据优化

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

我在NSDictionary中有大量用户,其结构如下:

        97 =             {
birthday = "";
gender = Unspecified;
image = {
status = Prepared;
type = Image;
};
"name_display" = "Facebook User";
"name_first" = Facebook;
"name_last" = User;
type = Contact;
"user_id" = 97;
};
98 = {
birthday = "";
gender = Unspecified;
image = {
status = Prepared;
type = Image;
};
"name_display" = "Facebook User";
"name_first" = Facebook;
"name_last" = User;
type = Contact;
"user_id" = 98
}

我想将此数据输入Core Data。首先,我必须检查用户是否已经存在于核心数据中。如果是这样,请更新该用户。否则,创建一个新用户。我这样做的方式有效,但是速度非常慢。这是我的代码:
NSDictionary *users = [responseData objectForKey:@"users"];
if (users) {
for (id userKey in [users allKeys]) {
NSDictionary *contactDictionary = [users objectForKey:userKey];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID == %@", userKey];
NSUInteger count = [CoreDataHelper countForEntity:@"Contact" withPredicate:predicate andContext:[FSAppDelegate managedObjectContext]];
if (count > 0) {
NSMutableArray *existingContactArray = [CoreDataHelper searchObjectsForEntity:@"Contact" withPredicate:predicate andSortKey:nil andSortAscending:NO andContext:[FSAppDelegate managedObjectContext]];
Contact *existingContact = [existingContactArray objectAtIndex:0];
[CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:existingContact];
}
else {
Contact *newContact = (Contact*)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:[FSAppDelegate managedObjectContext]];
[CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:newContact];
}
}

NSError *error;
if (![[FSAppDelegate managedObjectContext] save:&error]) {
// Handle the error.
NSLog(@"error saving to db - fsrequest class.");
}
}

这是我更新联系人的方法
+(BOOL)updateContactWithDictionary:(NSDictionary*)changedContact forContactObject:(Contact*)contact {
NSString *bday = [changedContact valueForKey:@"birthday"];
NSString *gender = [changedContact valueForKey:@"gender"];
NSString *nameDisplay = [changedContact valueForKey:@"name_display"];
NSString *nameFirst = [changedContact valueForKey:@"name_first"];
NSString *nameLast = [changedContact valueForKey:@"name_last"];
NSString *type = [changedContact valueForKey:@"type"];
NSString *userID = [NSString stringWithFormat:@"%@",[changedContact valueForKey:@"user_id"]];
NSString *imageStatus = [[changedContact objectForKey:@"image"]objectForKey:@"status"];
NSString *imageType = [[changedContact objectForKey:@"image"]objectForKey:@"type"];
NSString *imageURL = [[changedContact objectForKey:@"image"]objectForKey:@"url"];
NSString *imageThumb = [[changedContact objectForKey:@"image"]objectForKey:@"url_thumb"];
NSString *locationName = [[changedContact objectForKey:@"location"]objectForKey:@"name"];

[contact setBirthday:bday];
[contact setGender:gender];
[contact setNameDisplay:nameDisplay];
[contact setNameFirst:nameFirst];
[contact setNameLast:nameLast];
[contact setType:type];
[contact setUserID:userID];
[contact setImageStatus:imageStatus];
[contact setImageType:imageType];
if (imageURL && !((NSNull *)imageURL == [NSNull null])) {
[contact setImageURL:imageURL];
}
if (imageThumb && !((NSNull *)imageThumb == [NSNull null])) {
[contact setImageThumbURL:imageThumb];
}
if (locationName && !((NSNull *)locationName == [NSNull null])) {
[contact setLocationName:locationName];
}
return YES;
}

有人可以给我一个例子,说明我将如何以更快的方式执行此操作?有人提到了一些想法,但我需要看一下才能理解。谢谢!

最佳答案

首先,我将保存:循环外。更换:

 // save core data
NSError *error;
if (![[FSAppDelegate managedObjectContext] save:&error]) {
// Handle the error.
NSLog(@"error saving to db - fsrequest class.");
}
}
}


    }
// save core data
NSError *error;
if (![[FSAppDelegate managedObjectContext] save:&error]) {
// Handle the error.
NSLog(@"error saving to db - fsrequest class.");
}
}

另外,您是否在Core Data模型中为imageURL,imageThumb和locationName设置了一些默认值?如果否,为什么还要检查两次空值?

奖励:

消除countForEntity:withPredicate:andContext:调用可能是一个好主意,如下所示:
NSMutableArray *existingContactArray = [CoreDataHelper searchObjectsForEntity:@"Contact" withPredicate:predicate andSortKey:nil andSortAscending:NO andContext:[FSAppDelegate managedObjectContext]];

if ([existingContactArray count] > 0)
{
Contact *existingContact = [existingContactArray objectAtIndex:0];

[CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:existingContact];
}

关于iphone - 核心数据优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974717/

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