我正在開發和應用程序需要檢查一個字符串是否已被保存到數據庫中。這似乎是一個簡單的操作,但它需要半秒鐘才能返回我認爲相當多的任何響應。我的問題是,如果有什麼方法可以縮短這個時間。感謝您的關注。核心數據獲取請求優化
這是我當前的代碼:
- (BOOL) isDeleted:(int)i {
NSString *value = [NSString stringWithFormat:@"deleted.*.%i", i];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSString *entityName = @"Deleted";
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(deletedpics like %@)", value];
[request setPredicate:pred];
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
BOOL returnBool = [objects count] >= 1 ? YES : NO;
return returnBool;
}
你能解釋實體和關係嗎?我不太瞭解謂詞。 – 2013-03-03 20:06:50
不同實體之間沒有關係。具體而言,該實體只包含一個名爲_deletedpics_的屬性。參照謂詞,這個想法是檢查圖片是否已被報告爲已刪除或未被刪除。我將刪除的寄存器組織爲「deleted.status(我需要用於其他操作的變量).picturenumber」。在這個功能中,我想檢查某個圖片是否被刪除或沒有考慮到狀態。這就是我使用這個謂詞的原因。我希望現在更清楚。 – 2013-03-03 20:26:34
嘗試索引該屬性。 「@」刪除了什麼?*。%i「'是什麼意思? – 2013-03-03 21:11:37