ios - Fetching data from entity using nspredicate -
i have core data entity called "songs" contains details of different songs details. 1 of attribute of entity "language". want fetch songs language spanish. if number of songs spanish zero, should fetch songs default language, thats english. possible through single nspredicate if know default language , needed language.
you can way.
nsentitydescription *entity = [nsentitydescription entityforname:@"songs" inmanagedobjectcontext:managedobjectcontext]; nsfetchrequest *request = [[nsfetchrequest alloc] init]; //here strlanguage string 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) { (nsmanagedobject *info in mutablefetchcategory) { nslog(@"%@",info); //extract data each "info" object , store in array , display in tableview } } else { // repeat same code [or u can use functions reusability] language "english". }
you can refer link
Comments
Post a Comment