我有以下方法被稱爲在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
還有一件事 - 你的問題說目的是「創建和設置一個音符對象」。但是你的代碼似乎只是查找對象,而不是創建它們。你能澄清嗎? –