【发布时间】:2014-07-10 00:43:41
【问题描述】:
我在核心数据中设置了以下内容:包含许多成分的 Dish 实体和包含一个菜的成分实体。
我无法获取我的一对多关系数据,即某道菜的配料。我正在尝试使用与每道菜相关的成分填充我的收藏视图单元格。
我的核心数据模型设置如下:
我尝试了各种方法来获取数据。
我尝试过的事情之一是执行以下操作:Dish *dish = [self.fetchedResultsController objectAtIndexPath:indexPath];,然后是 NSLog disc.ingredients(它返回一个 NSSet),但返回整个核心数据项,即 createdAt 日期、名称以及所有内容,以及该菜的未格式化版本的配料。
我尝试的另一件事是使用Ingredient *ingredient = [dish.ingredients valueForKeyPath:@"ingredientName"]; 创建成分实体的新实例,但这会返回一个未格式化的字符串(我希望它在显示之前对其进行 UTF8 编码,因为它包含特殊字符)。
谷歌搜索时有很多解决方案建议使用 NSPredicate,但我就是不知道如何使用它。我对核心数据和 XCODE 还是很陌生,所以这里的任何帮助都将不胜感激,即你在哪里实现 NSPredicate,在我的情况下正确的谓词是什么?
一点支持代码:
我的 collectionViewController:
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSManagedObjectContext* context = [RKObjectManager sharedManager].managedObjectStore.mainQueueManagedObjectContext;
self.managedObjectContext = context;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dish" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
cellForItemAtIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FOFPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
self.object = [self.fetchedResultsController objectAtIndexPath:indexPath];
Dish *dish = [self.fetchedResultsController objectAtIndexPath:indexPath];
Ingredient *ingredient = ???;
cell.backgroundColor = [UIColor lightGrayColor];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"http://192.168.0.3:4000%@", dish.dishImage] description]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
cell.titleLabel.text = dish.dishName;
cell.headerLabel.text = dish.dishHeader;
cell.ingredientsLabel.text = [NSString stringWithFormat:@"%@", nil];
NSLog(@"%@", ingredient);
[self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
return cell;
}
【问题讨论】:
标签: ios objective-c core-data