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