我使用此代码选择与特定类别相关的所有产品,但它不起作用,我可用于获取与此类别相关的所有产品的正确谓词
注意: 我从服务器单独获取类别列表并插入它们,然后从服务器获取产品列表并插入它们,但我现在发现该产品不知道他的类别,我使用的是restkit库解析并插入数据库,所以如何告诉产品类别,restkit自动完成所有工作
如何设置我从服务器获得的产品列表?
//--fetching inserted Results from core data
// Getting products
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"productId" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor , nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category = %@ ",tempCategoryHolder];
// [fetchRequest setPredicate:predicate] ;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Product"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
fetchedObjectsProducts = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
答案 0 :(得分:0)
我会这样做,假设tempCategoryHolder持有categoryId:
NSError *error = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Product"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"category.categoryId = %@", tempCategoryHolder];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"productId" ascending:YES]];
NSArray *fetchedObjectsProducts = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
否则更新谓词以引用类别对象或不同的类别属性。