Note that there are some explanatory texts on larger screens.

plurals
  1. POCore data getting result based on entity count of relationship
    primarykey
    data
    text
    <p>I am new to core data and i dont know how to write query like this using core data.</p> <p>I hav two entities Album and Songs in schema. Album is related to Song by one many relationship and relationship name is tracks. I hav written query to fetch all albums and that is working well. But now i want to make little change in that so that i will not get albums having 0 songs. I tried to set predicate on request like </p> <pre><code>tracks.@count != 0 tracks.@count != nil </code></pre> <p>but this is not working may be because of faulting ?. Do i need to prefetch relationship or something. I dont want to add attribute songCount in Album and i just need count song. What is best way to write query like this ? thanks in advance !</p> <p>Code -</p> <pre><code>NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]]; NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; [request setSortDescriptors:[NSArray arrayWithObject:sort]]; NSPredicate *albumPredicate = [NSPredicate predicateWithFormat:@"tracks.@count &gt; 0"]; [request setPredicate:albumPredicate]; </code></pre> <p>This is what i got on console after turning on Debug mode</p> <pre><code>CoreData: sql: SELECT 0, t0.Z_PK FROM ZALBUM t0 WHERE (SELECT COUNT(*) FROM ZTRACK t1 WHERE (t0.Z_PK = t1.ZALBUM) ) &gt; ? ORDER BY t0.ZNAME COLLATE NSCollateNoCase </code></pre> <p>whats wrong in code ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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