【发布时间】:2018-01-26 09:02:14
【问题描述】:
我的 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_;
}
我还想确保通过 web 服务调用对部分选择的任何更改或文章列表的更新都应该自动更新满足条件的期刊列表,我不太确定如何适应与上述谓词相同。
其他信息:
之前忘了提,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"
}
]
【问题讨论】:
标签: ios cocoa-touch core-data nspredicate nsfetchedresultscontroller