简体   繁体   English

如何使用与NSFetchedResultsController的Coredata有序关系

[英]How to use Coredata ordered relationships with NSFetchedResultsController

  1. I have an Items Entity and a Tags Entity. 我有一个项目实体和一个标签实体。
  2. Items can have multiple Tags and Tags can be linked to multiple Items (many to many relationship). 项目可以有多个标签,标签可以链接到多个项目(多对多关系)。
  3. The relationship is an "Ordered Relationship" (using Ordered relationship in IOS5) both ways. 这种关系是两种方式的“有序关系”(在IOS5中使用有序关系)。

I want to fetch all Items for a Tag using a NSFetchedResultsController. 我想使用NSFetchedResultsController获取标签的所有项目。

Queries: 查询:

  1. Is it even possible to use ordered relationships with NSFetchRequest and NSFetchedResultsController? 是否可以使用NSFetchRequest和NSFetchedResultsController的有序关系?
  2. How do I specify the "Sort Descriptor"? 如何指定“排序描述符”?
  3. I tried two predicates. 我尝试了两个谓词。 The first did not give any results, the second one did give results. 第一个没有给出任何结果,第二个确实给出了结果。 Why does the first one does not work? 为什么第一个不起作用? Again how do I specify the sort descriptor to use the sorted order managed automatically by CoreData for this relationship? 同样,如何指定排序描述符以使用CoreData为此关系自动管理的排序顺序?

Did not work: [NSPredicate predicateWithFormat:@"ANY tags == %@", yyy]; 没有用:[NSPredicate predicateWithFormat:@“ANY tags ==%@”,yyy];

Worked: But less efficient: [NSPredicate predicateWithFormat:@"ANY tags.tagID == %@", xxx]; 工作:但效率较低:[NSPredicate predicateWithFormat:@“ANY tags.tagID ==%@”,xxx];

1 - Yes. 1 - 是的 It is possible. 有可能的。 When you create your mapping file, make sure you change the relationship's type from an NSSet to an NSArray, like so: 创建映射文件时,请确保将关系的类型从NSSet更改为NSArray,如下所示:

    @class Tags

    @interface Entity : NSManagedObject
    ...
    @property (nonatomic, retain) NSOrderedSet *tags;
    ...
    @end

    @interface Entity (CoreDataGeneratedAccessors)
    ...
    - (void)addEntityTags:(NSOrderedSet *)values;
    - (void)removeEntityTags:(NSOrderedSet *)values;
    ...
    @end

Change to: 改成:

    @class Tags

    @interface Entity : NSManagedObject
    ...
    @property (nonatomic, retain) NSArray *tags;
    ...
    @end

    @interface Entity (CoreDataGeneratedAccessors)
    ...
    - (void)addEntityTags:(NSArray *)values;
    - (void)removeEntityTags:(NSArray *)values;
    ...
    @end

2 - You can specify a sort descriptor when creating your fetch request, like so: 2 - 您可以在创建获取请求时指定排序描述符,如下所示:

    NSFetchRequest *fetchRequest [NSFetchRequest new];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:myManagedObjectContext];

    [fetchRequest.setEntity: entity];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"tags" ascending:<YES|NO>]];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY tags == %@", yyy]];

    NSFetchResultsController *myFetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:myManagedObjectContext setcionNameKeyPath:nil cacheName:nil];
    myFetchResultsController.delegate = self;

    [fetchRequest release]; // Forget that if using ARC.

    NSError *error = nil;

    if (![myFetchResultsController performFetch:&error]) {
        NSLog(@"Failed to fetch data: %@", error);
    }

3 - If you use the property, you shouldn't need to specify a predicate for your tags. 3 - 如果使用该属性,则不需要为标记指定谓词。 If you want to further sort your tags, you can simply use a sort descriptor on that array, like this: 如果要进一步对标记进行排序,可以在该数组上使用排序描述符,如下所示:

    NSSortDescriptor *tagDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"<tags field to sort on>" ascending:<YES|NO>];
    NSArray *sortedTags = [tagsProperty sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptors]];

You can simply read from that property for your tags. 您只需从该属性中读取标签即可。

Hope this helps! 希望这可以帮助!

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

相关问题 使用NSFetchedResultsController返回具有有序关系的对象 - Using NSFetchedResultsController to return objects with ordered relationships CoreData有序关系 - 使用NSFetchRequest批量取消 - CoreData ordered relationships - batch unfaulting using NSFetchRequest 如何正确使用 Codable、CoreData 和 NSSet 关系? - How do I properly use Codable, CoreData and NSSet relationships? 使用NSFetchedResultsController时,CoreData与UITableView有何关系? - How is CoreData related to UITableView when using NSFetchedResultsController? 带有 CoreData 和 NSFetchedResultsController 的 MVVM - MVVM with CoreData and NSFetchedResultsController CoreData和NSFetchedResultsController与合并更改 - CoreData and NSFetchedResultsController with merging changes CoreData,NSFetchedResultsController和performFetch: - CoreData, NSFetchedResultsController and performFetch: 使用NSFetchedResultsController更新CoreData对象 - Update CoreData Objects with NSFetchedResultsController 将 UICollectionView 与 CoreData 和 NSFetchedResultsController 一起使用 - Using UICollectionView with CoreData and NSFetchedResultsController 如何使用NSFetchedResultsController停止解雇的视图控制器来侦听coredata的更改? - How to stop a dismissed view controller using a NSFetchedResultsController to listen to coredata changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM