核心数据 - 谓词对多个对象

时间:2014-02-11 20:44:47

标签: ios iphone objective-c core-data nsfetchedresultscontroller

我有一个难以获取的查询,我不知道如何执行,
我的核心数据模型:

Artis
-----
name

songs -> (to-many relationship to Song Object)

Song
-----
title
release_Date

artist -> (to-one relationship to Artist Object)

我需要一个谓词来获取拥有最多歌曲(至少2首歌曲)的5位顶级艺术家,并且所有歌曲release_Date必须在过去30天内。

1 个答案:

答案 0 :(得分:0)

你需要结合:

// Predicate for date at least 30 days back

NSDate *date = [NSDate date];
NSDate *date30daysBack = [date dateByAddingTimeInterval:-60*60*24*30];

[NSPredicate predicateWithFormat: @"song.release_date >= %@", date30daysBack];

// Predicate for at least 2 songs

[NSPredicate predicateWithFormat: @"songs.@count >= 2"];

// Sort descriptor for most songs

//EDIT
//Sorting should be done after the fetch request.

// Set the fetchLimit of your NSFetchRequest to 5
request.fetchLimit = 5;