user2924482
user2924482

Reputation: 9120

Core Data: NSPredicate one to many to many relations

I have a core data model like this:

enter image description here

But I'm having problems trying to query all the names of the songs regarles of the albums or genre.

Any of you knows how can query all the songs in core data regardless of the album or genre?

I'll really appreciate your help.

Update:

NSManagedObjectContext *moc = [self managedObjectContext];  managedObjectContext];
    NSEntityDescription *description = [ NSEntityDescription entityForName:@"Song" inManagedObjectContext:moc];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Song"];
    request.entity = description;
    NSError *error = nil;
    NSArray *results = [moc executeFetchRequest:request error:&error];

The results array is empty. any of you knows why or what I'm doing wrong?

Upvotes: 0

Views: 69

Answers (2)

Mundi
Mundi

Reputation: 80265

Your fetch looks fine (just skip the entity description). Step through the code and make sure:

  • managed object context is not nil
  • result of executeFetchRequest is not nil but an NSArray
  • examine contents of the result
  • if it does not contain data, log the location of your persistent store to the console (when you set up your core data stack), cd to the documents folder and examine the sqlite file with the sqlite3 command line tool. Check that the records you are expecting are there.

If it works, it might be a problem with the display of your data rather than the retrieval.

Upvotes: 1

aahrens
aahrens

Reputation: 5590

Just create a fetch request for that CoreData model and execute the fetch

NSFetchRequest *request= [NSFetchRequest fetchRequestWithEntityName:@"Song"];

Upvotes: 0

Related Questions