Fetched Properties - Predicate

核能气质少年 提交于 2019-11-30 20:18:08

问题


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"]

回答1:


I had used a preloaded sqlite Database, which I copied from the bundle path at first launch and used to save user edits.

There's nothing wrong with shipping with pre-loaded data, per se. You should probably just ship a JSON file in the bundle and on initial app load, parse it and import it into Core Data. That will simplify what you're trying to do and won't be objectionable to Apple. The rest of your question becomes irrelevant if you do that. There's more than one way to do it.

Also, is there a reason you make Favorite its own entity? Why can't you just create a bool on the book entity called 'isFavorite'?




回答2:


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.



来源:https://stackoverflow.com/questions/10951844/fetched-properties-predicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!