我有一个简单的coredata实体,看起来像这样。
@interface Event : NSManagedObject
@property (nonatomic, retain) NSNumber id;
@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) id eventType; // this is a dictioary
@end
eventType属性是一个类似于
的字典eventType":{"id":1,"typeDescription":"Sale"}
我一直在尝试获取eventType为 typeDescription keyValue为“Sale”的所有事件对象
具有该值的实体存在于我的数据库中,但我得到的结果为零。
这是我执行fetch
的代码- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSManagedObjectContext *managedObjectContext = RKObjectManager.sharedManager.managedObjectStore.mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *eventTypePredicate=[NSPredicate predicateWithFormat:@"eventType.typeDescription == %@",@"Sale"];
NSCompoundPredicate *compoundPredicate ;
compoundPredicate=[[NSCompoundPredicate alloc]initWithType:NSAndPredicateType subpredicates:@[eventTypePredicate]];
[fetchRequest setPredicate:eventTypePredicate];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
答案 0 :(得分:0)
将核心数据存储在核心数据中的唯一方法是将其归档为NSData。
当您单击属性类型下拉列表时,您将看到支持的类型列表:
注意Dictionary不是其中之一。这是因为字典是一种非常复杂的对象类型,其树长在运行时未定义。由于Core Data是一个对象持久性框架,因此在尝试对它们运行查询之前,需要先了解所有对象。
因此,将字典存储在核心数据中的最大限制是您无法搜索嵌套在字典中的任何值,因为它们必须作为二进制数据保留。
因为你说你需要找到typeDescription值为" Sale"你已经部分回答了自己的问题。只需创建一个属性" typeDescription"用一种String类型。然后,您只需要搜索typeDescription = Sale
的所有实体