2

I am new to core data and i dont know how to write query like this using core data.

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

tracks.@count != 0 
tracks.@count != nil

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 !

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 > 0"];
 [request setPredicate:albumPredicate];

This is what i got on console after turning on Debug mode

CoreData: sql: SELECT 0, t0.Z_PK FROM ZALBUM t0 WHERE (SELECT COUNT(*) FROM ZTRACK t1 WHERE (t0.Z_PK = t1.ZALBUM) ) > ? ORDER BY t0.ZNAME COLLATE NSCollateNoCase 

whats wrong in code ?

Aditya Deshmane
  • 4,468
  • 2
  • 26
  • 32
  • 1
    `tracks.@count != 0` should just work. – Martin R Apr 26 '13 at 07:43
  • It'd help if you'd include your code and a better description of the problem. *this is not working* really isn't enough to let us help you figure out *why* it's not working. – Caleb Apr 26 '13 at 08:44

2 Answers2

2

Have you tried this:

[NSPredicate predicateWithFormat:@"tracks.@count>0"];

EDIT:

Try turning on SQLDebug and have a look at the generated SQL statement. You will find the statement in your console as your fetchrequest is being done. Here's how to do that: Add -com.apple.CoreData.SQLDebug 1 to the arguments passed at launch (under "Edit Scheme...")

gasparuff
  • 2,185
  • 24
  • 45
  • Both `"tracks.@count > 0"` and `"tracks.@count != 0"` should just work, there must be a different problem. – Martin R Apr 26 '13 at 08:43
0

EDITED: This will get all albums in your store that wont have 0 Songs / tracks

NSFetchRequest *fetchRequestItems = [[NSFetchRequest alloc] init];
NSEntityDescription *entityList = [NSEntityDescription entityForName:@"Albums" inManagedObjectContext:managedObjectContext];
[fetchRequestItems setEntity:entityList];

[fetchRequestItems setPredicate:[NSPredicate predicateWithFormat:@"tracks.@count != 0 "]];

//Sort by last edit ordered
NSArray *sortDescriptors = [NSArray arrayWithObjects:nil];
[fetchRequestItems setSortDescriptors:sortDescriptors];

NSUInteger count = [managedObjectContext countForFetchRequest:fetchRequestItems error:nil];
Dev2rights
  • 3,349
  • 3
  • 22
  • 42
  • 2
    I was editing it as you down voted, i misread and amended the error, at least give a guy a chance to correct his mistake before slamming him. – Dev2rights Apr 26 '13 at 08:36
  • 1
    Sorry, but checking for `Album == %@` does not make sense because the OP wants to fetch *all albums* that are related to at least one song. – Martin R Apr 26 '13 at 08:42
  • 2
    It is Apples recommendation to have an inverse relationship for both data integrity and also efficiency. iDev didn't specify that he wasn't using an inverse relationship too. xCode gives you a warning to say there its missing an inverse for a given data object too http://stackoverflow.com/questions/764125/does-every-core-data-relationship-have-to-have-an-inverse – Dev2rights Apr 26 '13 at 08:49
  • @Dev2rights: 1) Your code fetches all tracks that belong to the current album. That is not what OP asked about. 2) There is no "Tracks" entity in the question, only a "Song" entity. 3) The predicate `tracks.@count != 0` makes only sense if you fetch "Album" entities, not for Tracks/Song entities. – Martin R Apr 26 '13 at 08:59
  • Apologies, edited. And i think that in the schema "tracks" are what the OP has named the relationship of Songs in his Album object. – Dev2rights Apr 26 '13 at 09:02