从同一核心数据数据库中获取多个不同实体 [英] Fetching Multiple Different Entities from the same Core Data Database

查看:91
本文介绍了从同一核心数据数据库中获取多个不同实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个应用程序,其中有许多不同的实体,这些实体是从所有这些实体共有的基本模型中继承的。目前,让我说说我有EntityParent是我的基类,并且我有大约5个不同的Entities继承了基类。可以说我分别拥有EntityA,EntityB,EntityC,EntityD和EntityE,目前我正在从数据库中获取EntityA,而且我也想获取其他实体,如下所示:

  NSManagedObjectContext * context = _managedObjectContext; 

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entityDiscA = [NSEntityDescription entityForName:@ EntityA inManagedObjectContext:context];
[fetchRequest setEntity:entityDiscA];

NSError *错误;

NSArray * fetchedAs = [上下文executeFetchRequest:fetchRequest错误:&错误];


NSFetchRequest * fetchRequestB = [[NSFetchRequest alloc] init];
NSEntityDescription * entityDiscB = [NSEntityDescription entityForName:@ EntityB inManagedObjectContext:context];
[fetchRequestB setEntity:entityDiscB];

NSError *错误;

NSArray * fetchedBs = [上下文executeFetchRequest:fetchRequestB error:& error];

但是我也想获取EntityB和EntityC等,但是当我尝试获取我的应用程序崩溃。如果您看上面的代码,当我尝试获取EntityB时,会收到SigAbort并崩溃。上面的代码段有什么问题?我们是否不允许使用核心数据获取多个不同的实体?我唯一的选择是仅基于父类获取所有对象,然后使用类名进行过滤吗?我宁愿不这样做,但是如果那是唯一的出路,我想我将不得不努力并与之合作。不胜感激任何建议或技巧(我确实读过其中一篇文章,该人建议将父类声明为抽象实体,并使用其进行提取)




Rajan

解决方案

只需逐个获取所有实体。您可以简单地循环。

  NSMutableArray * allOfThem = [NSMutableArray array]; 
用于(@ [@ A,@ B,@ C,@ D,@ E]中的NSString * entitySuffix){
NSString * entity = [NSString stringWithFormat :@%@%@,@实体,entitySuffix];
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:entity];
[allOfThem addObjectsFromArray:
[上下文executeFetchRequest:请求错误:无]];
}

在创建获取请求之前,请确保所有实体名称都是正确的。 / p>

I am implementing an application where I have a bunch of different Entities which I inherit from a base model that is common to all these entities. Currently Lets say I have EntityParent who is my base class and I have about 5 different Entities inheriting the base class. Lets say I have EntityA, EntityB, EntityC, EntityD and EntityE respectively and currently I am fetching EntityA from the database and I also would like to fetch other entities just like Below:

NSManagedObjectContext *context = _managedObjectContext;

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entityDiscA = [NSEntityDescription entityForName:@"EntityA" inManagedObjectContext:context];
[fetchRequest setEntity:entityDiscA];

NSError* error;

NSArray *fetchedAs = [context executeFetchRequest:fetchRequest error:&error];


NSFetchRequest * fetchRequestB = [[NSFetchRequest alloc] init];
NSEntityDescription* entityDiscB = [NSEntityDescription entityForName:@"EntityB" inManagedObjectContext:context];
[fetchRequestB setEntity:entityDiscB];

NSError* error;

NSArray *fetchedBs = [context executeFetchRequest:fetchRequestB error:&error];

But I would like to also fetch EntityB's and EntityC's and so forth, but when I try to fetch my app crashes. If you look at the above code, when I try to fetch the EntityB, I get a SigAbort and crashes. Whats wrong with the code snippet above? Are we not allowed to fetch multiple different Entities using Core Data? Is my only option is to just fetch all the objects based on the parent class and then filter using the class name? I would prefer not to do that, but if thats the only way out, I guess I would have to suck up and work with it. Would appreciate any suggestions or tips (I did read one of the posts where the person suggests to declare the parent class as abstract entity and use it to do fetches)

Thanks, Rajan

解决方案

Just fetch all the entities one by one. You can simply loop.

NSMutableArray *allOfThem = [NSMutableArray array];
for (NSString *entitySuffix in @[@"A", @"B", @"C", @"D", @"E"]) {
   NSString *entity = [NSString stringWithFormat:@"%@%@", @"Entity", entitySuffix];
   NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entity];
   [allOfThem addObjectsFromArray:
      [context executeFetchRequest:request error:nil]];
}

Make sure that all the entity names are correct before creating the fetch request.

这篇关于从同一核心数据数据库中获取多个不同实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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