简体   繁体   English

如何处理与NSFetchedResultsController的关系?

[英]How do I handle a relationship with a NSFetchedResultsController?

Core data is already handling the relationship for me, do I need to build a new query to use the NSFetchedResultsController? 核心数据已经在为我处理这种关系,是否需要建立一个新查询以使用NSFetchedResultsController?

I have "Albums" that contain "Photos". 我有包含“照片”的“相册”。

Album *anAlbum = [...getalbum...];

in the *cellForItemAtIndexPath: I'd like to do something with: 在* cellForItemAtIndexPath中:我想使用以下方法:

anAlbum.photos

However, I can't convert the indexPath to a NSSet member index. 但是,我无法将indexPath转换为NSSet成员索引。 (obviously) (明显)

Without Core Data I'd typically just generate the query required myself. 如果没有Core Data,我通常只会自己生成所需的查询。 I'd like to make use of Core Data (again, obviously). 我想利用Core Data(显然也是)。

Usually, you're going to convert the set to an NSArray (maybe with NSSet's allobjects) and perhaps sort it or filter it with a NSSortDescriptor or NSPredicate or both. 通常,您将要将该集合转换为NSArray(也许带有NSSet的allobjects),并可能对其进行排序或使用NSSortDescriptor或NSPredicate或两者进行过滤。 Then, you can access the array by index using the index path section/row. 然后,您可以使用索引路径部分/行按索引访问数组。

You can obviously use a NSMutableArray if that's what you need, but it is a pain to try to access NSSet items individually as you essentially have to enumerate them to find what you're looking for. 显然,您可以根据需要使用NSMutableArray,但是尝试单独访问NSSet项非常麻烦,因为您必须枚举它们才能找到所需的内容。 Just create an array. 只需创建一个数组即可。 Here's an example where I've done this: 这是我执行此操作的示例:

NSArray *tableViewData = [NSArray arrayWithArray:[album.photos allObjects]];

Then, you just access the tableViewData objects with: 然后,您只需使用以下命令访问tableViewData对象:

UIImage *obj = UIImageJPEGRepresentation([tableViewData objectAtIndex:indexPath.row], 0.50f);

Something like that. 这样的事情。

The "Photo" entity (anAlbum.photos is the relationship) contains the asset url. “照片”实体(anAlbum.photos是关系)包含资产URL。 I have no issue with the displaying it was more of a concern of how do I use the NSSet (Core Data relationship) with the NSFectchedResultsController -- or direct with the view (collection/table). 我对显示没有问题,这更多地是关于如何将NSSet(核心数据关系)与NSFectchedResultsController一起使用-或直接与视图(集合/表)有关。

First of all I would use a NSFetchedResultsController for this. 首先,我将为此使用NSFetchedResultsController This component is made to work in conjunction with tables and allows to load data in a lazy loading manner. 使该组件与表结合使用,并允许以延迟加载的方式加载数据。 Second, the fetch request I would use should be run against Photo entity and not against Album . 其次,应该针对Photo实体而不是Album来运行我将使用的获取请求。 In other words, you should select all the photos that belong to a specific album. 换句话说,您应该选择属于特定相册的所有照片。

Here the code, I would use... 在这里的代码,我会用...

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
        entityForName:@"Photo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album == %@", anAlbum];
    [request setPredicate:predicate];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey:@"takeAt" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext sectionNameKeyPath:nil
            cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;

    return _fetchedResultsController;

}

So now in your tableView:cellForRowAtIndexPath: you would use 所以现在在您的tableView:cellForRowAtIndexPath:您将使用

Photo *photo = [_fetchedResultsController objectAtIndexPath:indexPath];
// access the album the photo belongs to
Album* album = photo.album;

You can grasp the main concepts for NSFetchedResultsController at Core Data Tutorial for iOS: How To Use NSFetchedResultsController . 您可以在iOS的核心数据教程中了解 NSFetchedResultsController的主要概念:如何使用NSFetchedResultsController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理NSFetchedResultsController提取错误? - How to handle NSFetchedResultsController fetch errors? 如何使用NSFetchedResultsController根据实体的关系获取结果? - How can I use NSFetchedResultsController to fetch results based on an entity's relationship? NSFetchedResultsController-如何处理更改核心数据源? - NSFetchedResultsController - how to handle changing Core Data source? NSFetchedResultsController一对多关系 - NSFetchedResultsController in one to many relationship NSFetchedResultsController委托回调关系 - NSFetchedResultsController delegate callback for relationship 如何在给定谓词的情况下使用NSFetchedResultsController返回关系属性的总数 - How to use NSFetchedResultsController to return total of a relationship property given a predicate NSFetchedResultsController:如何从实体的to-many关系中获取sectionName - NSFetchedResultsController: How to get sectionName from to-many relationship of the entity 如何处理coredata中的关系 - How to handle relationship in coredata 使用NSFetchedResultsController在各个视图之间快速编辑相关的Core Data对象/如何格式化NSPredicate并将其与块一起使用? - Swift editing related Core Data objects with NSFetchedResultsController across views / how do I format and use NSPredicate with a block? 核心数据排序关系+ NSFetchedResultsController - Core Data Sorted relationship + NSFetchedResultsController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM