本文介绍了核心数据库是空测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试核心数据库是否为空?
我试过:
How can I test if the core data database is empty?I tried:
NSIndexPath *path1 = [NSIndexPath indexPathForRow:0 inSection:0];
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:path1];
if([[managedObject valueForKey:@"date"] description]!=nil){SOMEFUNCTION}else{SOMEFUNCTION}
感谢
推荐答案
您必须为核心资料中使用的每个实体建立撷取要求。如果fetchrequest返回没有结果,您没有此实体的对象存储在您的核心数据中。
you have to create a fetchrequest for each entity you use in core data. if the fetchrequest returns without results you don't have objects of this entity stored in your core data.
- (BOOL)coreDataHasEntriesForEntityName:(NSString *)entityName {
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
[request setFetchLimit:1];
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];
if (!results) {
LogError(@"Fetch error: %@", error);
abort();
}
if ([results count] == 0) {
return NO;
}
return YES;
}
这篇关于核心数据库是空测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!