使用获取的属性进行核心数据跨商店查询 [英] Core data cross-store query using fetched property

查看:41
本文介绍了使用获取的属性进行核心数据跨商店查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有一个核心数据数据库,该数据库由两个存储组成(一个存储我的数据的存储库,一个存储用户数据的存储),并通过获取的属性在它们之间链接.

假设我有两个实体,因此它们之间的关系为0到1.卡0 ----> 1 CardStatus

1),其中包含参考数据和一些属性(externalKey,单词,描述等)

获取属性-userData(指向CardStatus并使用externalKey进行2个实体之间的链接)

2) CardStatus ,其中包含每张卡的状态.该实体存储在用户数据存储区中.

在某些情况下,我需要根据卡的状态来获取卡(例如:检索所有标记为过期的卡,检索所有标记为新卡的卡等等)

问题

我应该基于CardStatus实体的值编写哪种谓词来获取Card实体?

我尝试直接在谓词中使用获取的属性userData,但核心数据不允许使用该属性.

由于未捕获的异常而终止应用程序"NSInvalidArgumentException",原因:找不到关键路径userData"实体'

然后我尝试了一个子查询(如下所示)-仍然无法正常工作.有趣的是,当我直接在NSArray上执行过滤器(而不是获取请求)时,它可以正常工作.

示例-检索具有特定状态的卡:

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@(SUBQUERY(userData,$ x,($ x.currentLevel == 0)).@ count> 0)"]]; 

示例-检索标记为新卡(即CardStatus中没有数据):

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(SELF,$ x,$ x.userData.@ count == 0).@ count> 0"]]; 

有什么建议吗?

解决方案

我找到了解决方案.

查询是正确的,现在可以正常工作.问题在于我在线程中使用了一个托管对象子项,而这些子上下文似乎不接受使用获取的属性.

我用(简单地)直接基于我的持久性存储创建了一个新的管理对象上下文(换句话说,这个上下文和我在主线程中使用的上下文在层次结构中处于同一级别).

以下是用于说明我所描述内容的代码:

  AppDelegate * appDelegate =(AppDelegate *)[UIApplication sharedApplication] .delegate;NSManagedObjectContext * child = [[NSManagedObjectContext alloc] init];[子setPersistentStoreCoordinator:[appDelegate.managedObjectContextpersistentStoreCoordinator]]; 

现在一切正常.

Background

I've got a core data database made of two stores (one repository for my data and one store for the user data), linked between them by fetched properties.

Let's say I have two entities such that the relationship between them is 0 to 1. Card 0 ----> 1 CardStatus

1) Card, containing reference data and some properties (externalKey, word, description, ...)

Fetch Properties - userData (pointing to CardStatus and using the externalKey to do the link between the 2 entities)

2) CardStatus, containing the status of each Card. This entity is stored in the user data store.

There are cases where I need to fetch cards based on their status (example: retrieve all the cards that are marked as expired, retrieved all the cards that are marked as new, etc...)

Question

What kind of predicate should I write to fetch Card entities based on values from the CardStatus entity?

I tried using my fetched property userData directly in the predicate, but it's not allowed by core data.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath userData not found in entity '

Then I tried with a subquery (see below) - still doesn't work. Interestingly, it works fine when I do a filter directly on an NSArray (instead of a fetch request).

Example - Retrieving card with a specific status:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(userData, $x, ($x.currentLevel == 0)).@count > 0)"];

Example - Retrieving card marked as new (i.e. no data in CardStatus):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(SELF, $x, $x.userData.@count == 0).@count > 0"];

Any suggestion?

解决方案

I found the solution.

The query was correct and is now working fine. The problem was that I was using a child managedObjectContext in my thread, while those children contexts don't seem to accept the use of fetched properties.

I replaced this by (simply) creating a new managed object context directly based on my persistent store (in other words, this context and the context I'm using in the main thread are at the same level in the hierarchy).

Below is the code to illustrate what I'm describing:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *child = [[NSManagedObjectContext alloc] init];
[child setPersistentStoreCoordinator:[appDelegate.managedObjectContext persistentStoreCoordinator]];

It's now working all fine.

这篇关于使用获取的属性进行核心数据跨商店查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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