0
我在我的數據庫類中有一個方法,用於驗證給定的值是否存在於實體中。除了我正在試圖優化這種方法之外,一切工作都很好。你們可以讓我知道這個代碼是否可以進一步優化?提高核心數據重複驗證的效率
- (NSUInteger)recordAlreadyExists:(NSString*)string forEntity:(NSString*)entityName forKey:(NSString*)key
{
NSManagedObjectContext *newContext = [Helper generateNewContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:newContext];
[newContext setUndoManager:nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
[fetchRequest setFetchLimit:1];
//NSString *predicateString = [NSString stringWithFormat:@"%K LIKE \"%@\"", key, string];//,key,string];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", key, string];
[fetchRequest setPredicate:predicate];
NSError *error;
NSUInteger resultsCount = [newContext countForFetchRequest:fetchRequest error:&error];
if(resultsCount)
return resultsCount;
return 0;
}
乾杯
謝謝@Martin。我確實看到了性能的顯着提高:) – iSee 2013-02-27 01:59:59