我的coredata xcdatamodel中有以下设置:
网站上基本上有一套文章(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_;
}
但是,我仍然坚持创建FRC,只获取那些属于用户已经选择了部分的文章的期刊,我的FRC的占位符谓词:
@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调用对文章列表中选择部分或更新的任何更改都应自动更新满足条件的期刊列表,我不太清楚如何容纳在上面的谓语中也是如此。
其他信息:
忘了早些时候提到 taxonomyID 是Sections(TMSection)和Journals(TMJournal)的主键,而 nid 是文章的主键(TMArticle)
每个人的Web服务响应示例:
文章列表:
[
{
"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 & 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"
}
]
答案 0 :(得分:2)
你应该几乎总是指定每个关系的反转;很少有情况下你没有好转。
查看您的数据,似乎某个特定部分可能与许多文章相关,因此请添加从TMSection
到TMArticle
的多对多关系。
数据只有与一篇文章相关的期刊;如果这是普遍正确的,那么您可以添加从TMJournal
到TMArticle
的一对一关系。
然后使用以下谓词来获取TMJournals
:
[journalFR setPredicate:[NSPredicate predicateWithFormat:@"ANY article.sections == %@", self.selectedSectionManagedObjectID];
可悲的是,FRC只会观察Journal
个对象的变化,因此您需要专门处理对文章或选择Section
的更改。