NSFetchedResultsController和实体继承 [英] NSFetchedResultsController and Entity Inheritance

查看:137
本文介绍了NSFetchedResultsController和实体继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模型事件中有一个父实体。和两个孩子实体:生日,周年。我使用内置到Core数据的实体继承功能,使生日和周年纪念的父对象是事件。



因此我使用以下方式进行提取:

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription * entity = [NSEntityDescription entityForName:@EventinManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//将批量大小设置为合适的数量。
[fetchRequest setFetchBatchSize:20];

//根据需要编辑排序键。
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@start_dateascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObjects:sortDescriptor,nil];

etc ...



现在我想按事件对fetchedObjects进行排序类型,生日或周年纪念。



我该怎么办?我尝试了:

  for(Birthday * birthday in _fetchedResultsController.fetchedObjects){
[birthdayObjects addObject:birthday];
}
(周年纪念日在_fetchedResultsController.fetchedObjects中){
[anniversaryObjects addObject:anniversary];
}

但这只是将fetchedObjects中的所有对象添加到每个数组。



有任何想法或我会以错误的方式进行吗?



UPDATE

  for(Event *事件] {
if([event isKindOfClass:[Birthday class]]){
[birthdayObjects addObject:event];
} else {
[anniversary addObject:event];
}
}

但是如果有更好的方式它!谢谢。

解决方案

事件上设置只读属性,称为 eventType 或类似的,返回一个字符串。你的Event类返回一个字符串,你的子事件返回生日,周年纪念,对于未来的事件类型。



在获取的结果控制器中,如果使用一个,你可以将eventType设置为你的区域名称键路径,这将把类型划分为多个部分,这对于此目的是很好的,但是您不能将它们用作提取请求的一部分。



在上述情况下,使用事件类型作为谓词或排序描述符来处理从获取请求返回的所有事件的数组。



如果您不喜欢字符串,可以使用枚举。如果要在获取请求中使用事件类型,请将其作为Event类的属性,并在awakeFromInsert上适当地设置它。


I have a parent entity in my model Event. And two child entities: Birthday, Anniversary. I'm using the entity inheritance feature built into Core data such that birthday and anniversary's parent object is Event.

So I do a fetch using the following:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"start_date" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

etc...

Now I want to sort the fetchedObjects by event type, birthday or anniversary.

How do I do that? I tried:

for (Birthday *birthday in _fetchedResultsController.fetchedObjects){
    [birthdayObjects addObject:birthday]; 
}
for (Anniversary *anniversary in _fetchedResultsController.fetchedObjects){
    [anniversaryObjects addObject:anniversary]; 
}

But this just adds all objects in the fetchedObjects to each array.

Any ideas or am I going about this the wrong way?

UPDATE:

Figured it out using this:

for (Event *event in _fetchedResultsController.fetchedObjects){
    if ([event isKindOfClass:[Birthday class]]){
        [birthdayObjects addObject:event]; 
    }else{
        [anniversary addObject:event];
    }
}

But if there is a better way I am open to it! Thanks.

解决方案

Have a read-only property on Event, called eventType or similar, which returns a string. Your Event class returns one string, and your child events return Birthday, Anniversary, whatever else for future event types.

In your fetched results controller, if you use one, you can set eventType as your section name key path and this will divide the types into sections for you - transient properties like this are fine for this purpose, but you can't use them as part of the fetch request.

In the situation above, use the event type as a predicate or sort descriptor to process the array of all events that comes back from the fetch request.

If you don't like a string, you could use an enum. If you want to use the event type in a fetch request, make it an attribute of the Event class and set it appropriately on awakeFromInsert.

这篇关于NSFetchedResultsController和实体继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆