代码之家  ›  专栏  ›  技术社区  ›  Raj Pawan Gumdal

一对多关系上的Coredata NSFetchedResultsController谓词(由外向内)

  •  1
  • Raj Pawan Gumdal  · 技术社区  · 8 年前

    我的coredata xcdatamodel中有以下设置:

    enter image description here

    基本上,网站上有一组文章(TMArticle),这些文章将被分为章节(TMSection)和期刊(TMJournal)。应用程序导航必须先显示所有部分,然后 . 我不太确定如果没有相反的关系,我如何才能做到这一点。理想情况下,在这种设置中不需要反向关系(从章节和期刊返回到文章),但我如何才能编写一个谓词,它完全满足我的要求?

    我使用以下FRC获得所有文章的列表:

    @synthesize articlesFRC = articlesFRC_;
    -(NSFetchedResultsController*)articlesFRC {
        if (nil==articlesFRC_) {
            NSFetchRequest *articlesFR = [NSFetchRequest fetchRequestWithEntityName:[TMArticle entityName]];
            NSSortDescriptor *articleSD = [NSSortDescriptor sortDescriptorWithKey:@"nid"
                                                                        ascending:NO];
            [articlesFR setSortDescriptors:[NSArray arrayWithObject:articleSD]];
            articlesFRC_ = [[NSFetchedResultsController alloc] initWithFetchRequest:articlesFR
                                                               managedObjectContext:[[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread]
                                                                 sectionNameKeyPath:nil
                                                                          cacheName:nil];
            [articlesFRC_ setDelegate:self];
            [articlesFRC_ performFetch:NULL];
        }
        return articlesFRC_;
    }
    

    @synthesize journalsFRC = journalsFRC_;
    -(NSFetchedResultsController*)journalsFRC {
        if (nil==journalsFRC_ && nil!=self.selectedSectionManagedObjectID) {
            NSFetchRequest *journalFR = [NSFetchRequest fetchRequestWithEntityName:[TMJournal entityName]];
            NSSortDescriptor *journalSD = [NSSortDescriptor sortDescriptorWithKey:@"weight"
                                                                        ascending:YES];
            [journalFR setSortDescriptors:[NSArray arrayWithObject:journalSD]];
            [journalFR setPredicate:<#(NSPredicate * _Nullable)#>];
            journalsFRC_ = [[NSFetchedResultsController alloc] initWithFetchRequest:journalFR
                                                               managedObjectContext:[[[RKObjectManager sharedManager] objectStore] managedObjectContextForCurrentThread]
                                                                 sectionNameKeyPath:nil
                                                                          cacheName:nil];
            [journalsFRC_ setDelegate:self];
            [journalsFRC_ performFetch:NULL];
        }
        return journalsFRC_;
    }
    

    我还想确保通过webservice调用对章节选择的任何更改或文章列表的更新都会自动更新满足条件的期刊列表,我不太确定如何在上面的谓词中适应相同的内容。

    其他信息 :

    之前忘了提 是章节(TM部分)和期刊(TMJournal)的主键,而 nid公司

    每个示例的webservice响应:

    [
        {
            "nid": "3",
            "listing_post_date": "Tuesday, August 15, 2017 - 19:49",
            "listing_updated_date": "Tuesday, August 15, 2017 - 19:49",
            "title": "ACL Tear",
            "field_journal": [
                {
                    "tid": "6"
                },
                {
                    "tid": "7"
                }
            ],
            "field_section": [
                {
                    "tid": "2"
                },
                {
                    "tid": "8"
                }
            ]
        },
        {
            "nid": "1",
            "listing_post_date": "Saturday, March 25, 2017 - 14:23",
            "listing_updated_date": "Sunday, April 9, 2017 - 10:51",
            "title": "Reverse Total Shoulder arthroplasty",
            "field_journal": [
                {
                    "tid": "3"
                },
                {
                    "tid": "4"
                }
            ],
            "field_section": [
                {
                    "tid": "2"
                },
                {
                    "tid": "5"
                }
            ]
        }
    ]
    

    章节列表:

    [
        {
            "name": "Sports",
            "field_section_image": [],
            "tid": "2",
            "weight": "0",
            "field_section_color": {
                "rgb": "#bbf1f3"
            }
        },
        {
            "name": "Hand",
            "field_section_image": [],
            "tid": "5",
            "weight": "1",
            "field_section_color": {
                "rgb": "#96c394"
            }
        },
        {
            "name": "Knee",
            "field_section_image": [],
            "tid": "8",
            "weight": "2",
            "field_section_color": {
                "rgb": "#f29d9d"
            }
        }
    ]
    

    [
        {
            "name": "Bone &amp; Joint Research",
            "tid": "3",
            "weight": "0"
        },
        {
            "name": "Sports Journal",
            "tid": "7",
            "weight": "0"
        },
        {
            "name": "Sports Medicine",
            "tid": "4",
            "weight": "0"
        },
        {
            "name": "The Knee Journal",
            "tid": "6",
            "weight": "0"
        }
    ]
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   pbasdf    8 年前

    你几乎总是应该为每一种关系指定相反的结果;很少有情况下,没有它你会过得更好。

    TMSection TMArticle

    TMJournal .

    然后使用以下谓词获取 TMJournals

    [journalFR setPredicate:[NSPredicate predicateWithFormat:@"ANY article.sections == %@", self.selectedSectionManagedObjectID];
    

    不幸的是,FRC只会观察到 Journal 对象,因此您需要专门处理对项目或选择的更改 Section