我正在尝试构建通用SyncEngine
。在其中,我按如下方式获取给定的父实体:
__block NSArray *newRecords = nil;
[[[CoreDataController sharedInstance] managedObjectContext] performBlockAndWait:^{
NSError *error = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:className];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"serverID = nil"];
[request setPredicate:predicate];
newRecords = [[[CoreDataController sharedInstance] managedObjectContext] executeFetchRequest:request error:&error];
}];
我正在寻找以下方法:
有办法吗?
答案 0 :(得分:0)
您的子实体属于同一基类
在类型为NSString
的基类上添加一个属性(我们称之为className
)。
在<BaseEntity>.h
中(生成文件转到编辑器 - >创建NSManagedObject子类):
像这样实施- (void) awakeFromInsert
:
- (void) awakeFromInsert
{
self.className = self.entity.name;
}
您的提取请求现在应该是:
NSExpressionDescription* idDesc = [NSExpressionDescription new];
[idDesc setName:@"objectID"];
[idDesc setExpression:[NSExpression expressionForEvaluatedObject]];
[idDesc setExpressionResultType:NSObjectIDAttributeType];
NSFetchRequest* request = [NSFetchRequest alloc] initWithEntityName:@"<BaseEntity>"];
request.predicate = [NSPredicate predicateWithFormat:@"parent = %@",parent.objectID];
[request setResultsType:NSDictionaryResultType];
[request setPropertiesToFetch:@[@"className",idDesc]];
此请求将返回包含您请求的数据的字典数组。