0
我有一个名为“歌曲”的核心数据实体,其中包含不同歌曲细节的详细信息。这个实体的一个属性是“语言”。我想用语言西班牙语获取所有歌曲。但是如果有西班牙语的歌曲数量为零,那么它应该使用默认语言获取所有歌曲,即英文。如果我知道默认语言和所需的语言,是否可以通过单个NSPredicate实现?使用nspredicate从实体获取数据
我有一个名为“歌曲”的核心数据实体,其中包含不同歌曲细节的详细信息。这个实体的一个属性是“语言”。我想用语言西班牙语获取所有歌曲。但是如果有西班牙语的歌曲数量为零,那么它应该使用默认语言获取所有歌曲,即英文。如果我知道默认语言和所需的语言,是否可以通过单个NSPredicate实现?使用nspredicate从实体获取数据
你可以这样做。
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//here strLanguage is string with the name of selected language
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"Language == %@",strLanguage];
[request setPredicate:predicate];
[request setEntity:entity];
NSMutableArray* mutableFetchCategory = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if ([mutableFetchCategory count] > 0)
{
for (NSManagedObject *info in mutableFetchCategory)
{
NSLog(@"%@",info);
//Extract data from each "info" object and store it in an array and display it in tableview
}
}
else
{
// Repeat the same code [or u can use functions for reusability] with language "English".
}
您也可以参考这个link