Note that there are some explanatory texts on larger screens.

plurals
  1. POFetched Properties - Predicate
    text
    copied!<p>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.</p> <h1>SAMPLE PROJECT</h1> <p>The need to use Fetched Properties came, because my app was rejected because of violating the iOS Data Storage Guidelines. In <a href="http://developer.apple.com/library/ios/#qa/qa1719/_index.html" rel="nofollow noreferrer">QA1719</a> they write:</p> <blockquote> <p>Apps should avoid mingling app data and user data in the same file.</p> </blockquote> <p>I think this may be necessary for easier ICloud sync and may also help in migration.</p> <p>So, how to do this?</p> <p>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.</p> <p>The main problem is, that it isn't possible to have relations across different stores (<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW5" rel="nofollow noreferrer">Cross-Store Relationships</a>).</p> <p>I haven't found a sample for a Fetched Property Project yet, so i'm here trying to make my own.</p> <p>For the sample there is:</p> <ol> <li>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</li> <li>a way to make books your favorite ones. This is stored in the persistent store UserData.sqlite.</li> </ol> <p>The entities are:</p> <pre><code>-------------- -------------- | Books | | Favorite | -------------- -------------- - author - favorite - title --- 1 : 1 --- - books_title </code></pre> <p>Entity 'Books' is for PreloadedData, 'Favorite' should contain UserData.</p> <p><em>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...</em></p> <p>In my xcdatamodeld (Xcode Editor) I have added two Configurations (PreloadStore, UserStore). Each contains only contains the Entity needed.</p> <p>They are used with (everything is else is from boilerplate):</p> <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { ... if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"PreloadStore" URL:storeURL options:nil error:&amp;error]) { ... if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"UserStore" URL:storeURLUserData options:nil error:&amp;error]) { </code></pre> <p>I made a fetched property link_favorite in Books with the Predicate</p> <pre><code>books_title == $FETCH_SOURCE.title </code></pre> <p>The sample project can be <strong>downloaded <a href="https://www.dropbox.com/s/fup0v370qtsbvgz/Books.zip" rel="nofollow noreferrer">here</a></strong>. It's working, except for the Predicate (filter by Favorite books).</p> <h1>MY QUESTION</h1> <p>I'd like to filter my fetch in Books by only those books, which are favorite. I tried using</p> <pre><code>fetchRequest.predicate = [NSPredicate predicateWithFormat:@"favorite_link.favorite == 1"]; </code></pre> <p>in <code>MasterViewController.m</code> (fetchedResultsController), but this only leads to a crash.</p> <p>I think I must use something <a href="https://stackoverflow.com/questions/6449615/fetched-properties-inside-nsfetchedresultscontrollers-predicate">like here</a> with a COUNT or a SUBQUERY, but I couldn't figure it out.</p> <p>Thanks in advance for your help!</p> <p><strong>--edit:</strong></p> <p>I made a simple NSArray fetch (Core Data), here the following is working:</p> <pre><code>NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY link_favorite.favorite == 1"]; result = [result filteredArrayUsingPredicate:predicate]; </code></pre> <p>But how do I translate this in a SUBQUERY for the NSFetchedResultsController (<a href="http://funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery" rel="nofollow noreferrer">tried this blog article</a>)? Wrong is:</p> <pre><code>NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(contents, $x, $x.link_favorite.favorite == 1).@count &gt; 0"] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload