获取属性 - 谓词 [英] Fetched Properties - Predicate

查看:135
本文介绍了获取属性 - 谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在NSPredicate中使用获取的属性(见下面的我的问题)。它不是关于谓词获取获取的属性。



示例项目



需要使用Fetched属性,因为我的应用程式因违反iOS资料储存指南而遭到拒绝。在 QA1719 中,他们写下:


应用程式应避免在同一个档案中混用应用程式资料和使用者资料。


我认为这可能是必要的更容易的ICloud同步,也可能有助于迁移。



那么,如何这样做?



我使用了一个预加载的sqlite数据库,我在第一次启动时从软件包路径复制,用于保存用户编辑。因为你不应该混合预加载和用户数据,我试图分离它们。



主要的问题是,不可能有不同的关系商店(跨商店关系)。



我还没有为Fetched Property Project找到样本,所以我在这里试图我自己的。



对于示例,有:


  1. 图书(在样例中,您使用加号图标预加载它们)存储在持久存储Books.sqlite和

  2. 中,这是一种使书籍成为您最喜欢的书籍的方法。这是存储在持久存储UserData.sqlite。

实体是:

  -------------- -------------- 
|图书| |喜欢|
-------------- --------------
- author - favorite
- title --- 1 :1 --- - books_title

实体书籍用于PreloadedData,收藏夹应包含UserData



喜欢标签只是一个例子,它也可以是用户制作的Notes(注释可用),或者如果该书租给其他人...



在我的xcdatamodeld(Xcode Editor)中,我添加了两个配置(PreloadStore,UserStore)。每个只包含所需的实体。



它们用于(一切都是来自样板):

   - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
...
if(![__ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@PreloadStoreURL:storeURL options:nil error: & error]){
...
if(![__ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@UserStoreURL:storeURLUserData options:nil error:& error]){



我在书籍中使用谓语

创建了一个抓取的属性link_favorite

  books_title == $ FETCH_SOURCE.title 

示例项目已下载此处



我的问题



我想要的过滤我的书中的书籍只有那些书,这是最喜欢的。我尝试使用

  fetchRequest.predicate = [NSPredicate predicateWithFormat:@favorite_link.favorite == 1];在 MasterViewController.m (fetchedResultsController)中的



<但这只会导致崩溃。



我想我必须使用

预先感谢您的帮助,您可以在这里找到一个包含COUNT或SUBQUERY的内部nsfetchedresultscontrollers- !



- 编辑



我做了一个简单的NSArray提取数据),这里是以下是工作:

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY link_favorite.favorite == 1 ]; 
result = [result filteredArrayUsingPredicate:predicate];但是如何在SUBQUERY中翻译这个NSFetchedResultsController(
/funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subqueryrel =nofollow>试过这篇博客文章)?错误是:

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@SUBQUERY(contents,$ x,$ x.link_favorite.favorite == 1)。@ count> 0] 


解决方案

子查询需要一个我们在这里没有的关系。所以,我不会工作在我看来。
您可以在fetchedResultsController中切换实体,通过获取属性将所需的数据检索到相关的books对象。
如果要节省时间,您可以(如您所做的)存储最相关的数据,以便在您最喜欢的对象中的表视图中显示,并且只有在用户选择单元格并进入详细视图时检索其他数据。


I have a question about using fetched properties inside a NSPredicate (see below 'My question'). It's not about the predicate to get the fetched property. For clarification and for others, which may also face this problem I tried to make a small sample project.

SAMPLE PROJECT

The need to use Fetched Properties came, because my app was rejected because of violating the iOS Data Storage Guidelines. In QA1719 they write:

Apps should avoid mingling app data and user data in the same file.

I think this may be necessary for easier ICloud sync and may also help in migration.

So, how to do this?

I had used a preloaded sqlite Database, which I copied from the bundle path at first launch and used to save user edits. Because you shouldn't mix preloaded and user data, I'm trying to seperate them.

The main problem is, that it isn't possible to have relations across different stores (Cross-Store Relationships).

I haven't found a sample for a Fetched Property Project yet, so i'm here trying to make my own.

For the sample there is:

  1. a preloaded list of books (in the sample you 'preload' them with the Plus Icon) which is stored in the persistent store Books.sqlite and
  2. a way to make books your favorite ones. This is stored in the persistent store UserData.sqlite.

The entities are:

--------------                --------------
|   Books    |                |   Favorite |
--------------                --------------
- author                      - favorite
- title        --- 1 : 1 ---  - books_title

Entity 'Books' is for PreloadedData, 'Favorite' should contain UserData.

The Favorite-Tag is just an example, it may also be User made Notes (Predicate for Note available) or if the book is rented to somebody else...

In my xcdatamodeld (Xcode Editor) I have added two Configurations (PreloadStore, UserStore). Each contains only contains the Entity needed.

They are used with (everything is else is from boilerplate):

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
...
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"PreloadStore" URL:storeURL options:nil error:&error]) {
...
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"UserStore" URL:storeURLUserData options:nil error:&error]) {

I made a fetched property link_favorite in Books with the Predicate

books_title == $FETCH_SOURCE.title

The sample project can be downloaded here. It's working, except for the Predicate (filter by Favorite books).

MY QUESTION

I'd like to filter my fetch in Books by only those books, which are favorite. I tried using

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"favorite_link.favorite == 1"];

in MasterViewController.m (fetchedResultsController), but this only leads to a crash.

I think I must use something like here with a COUNT or a SUBQUERY, but I couldn't figure it out.

Thanks in advance for your help!

--edit:

I made a simple NSArray fetch (Core Data), here the following is working:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY link_favorite.favorite == 1"];
result = [result filteredArrayUsingPredicate:predicate];

But how do I translate this in a SUBQUERY for the NSFetchedResultsController (tried this blog article)? Wrong is:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(contents, $x, $x.link_favorite.favorite == 1).@count > 0"]

解决方案

subqueries need a relationship which we do not have here. So that wont work in my opinion. You could switch the entity in your fetchedResultsController retrieve the data you need via a fetched property to the related books object. If to save time you can (as you do already) store the most relevant data for you to display in the tableview within your favorite object and retrieve additional data only if the user selects a cell and you go into a detailed view.

这篇关于获取属性 - 谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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