我正在开发和应用程序需要检查一个字符串是否已被保存到数据库中。这似乎是一个简单的操作,但它需要半秒钟才能返回我认为相当多的任何响应。我的问题是,如果有什么方法可以缩短这个时间。感谢您的关注。核心数据获取请求优化
这是我当前的代码:
- (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