我有以下方法被称为在for循环内并且被调用多次,每次通过一个NSDictionary对象迭代创建和设置的注释对象:优化核心数据/神奇记录 - findFirstByAttribute - 核心数据
- (BOOL)updateById:(NSString *)entityId
withData:(NSDictionary *)dataDictionary {
DLog(@"Updating %@", [_entityClass description]);
if (_entityIdentifier == nil) {
DLog(@"entityIdentifier has not been set");
}
NSManagedObjectContext *context = ContextForThread;
id note = [_entityClass findFirstByAttribute:_entityIdentifier
withValue:entityId
inContext:context]; //This is running slowly ?
[note setValuesFromDictionary:dataDictionary];
BOOL changes = YES;
if ([note changedValues].count == 0) {
changes = NO;
DLog(@"Has NOT changed - Dont save");
}
else {
DLog(@"Has changed");
}
return changes;
}
我想优化这些代码,并已经注意到findFirstByAttribute方法显得较为缓慢。无论如何,我可以优化这种方法吗?
每当你在循环中调用这个方法时,'entityID'是否相同? –
嗨汤姆 - 每次都不一样。 – GuybrushThreepwood
还有一件事 - 你的问题说目的是“创建和设置一个音符对象”。但是你的代码似乎只是查找对象,而不是创建它们。你能澄清吗? –