Coredata NSFetchedResultsController 谓词一对多关系(从外到内)
Coredata NSFetchedResultsController predicate on One-To-Many relationship (outside in)
我的 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_;
}
我还想确保通过网络服务调用对部分选择的任何更改或文章列表的更新应该自动更新满足条件的期刊列表,我不太确定如何才能在上面的谓词中容纳相同的内容。
附加信息:
前面忘了说 taxonomyID 是 Sections (TMSection) 和 Journals (TMJournal) 的主键,而 nid 是文章 (TMArticle) 的主键。
每个示例网络服务响应:
文章列表:
[
{
"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"
}
]
您应该几乎总是为每个关系指定逆;在极少数情况下,没有你会过得更好。
看你的数据,似乎给定的部分可以与很多文章相关,所以添加从TMSection
到TMArticle
的一对多关系。
数据只有一篇文章相关的期刊;如果这是普遍正确的,那么您可以添加从 TMJournal
到 TMArticle
.
的一对一关系
然后使用以下谓词来获取 TMJournals
:
[journalFR setPredicate:[NSPredicate predicateWithFormat:@"ANY article.sections == %@", self.selectedSectionManagedObjectID];
遗憾的是,FRC 只会观察 Journal
对象的变化,因此您需要专门处理对文章或选择 Section
.
的变化
我的 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_;
}
我还想确保通过网络服务调用对部分选择的任何更改或文章列表的更新应该自动更新满足条件的期刊列表,我不太确定如何才能在上面的谓词中容纳相同的内容。
附加信息:
前面忘了说 taxonomyID 是 Sections (TMSection) 和 Journals (TMJournal) 的主键,而 nid 是文章 (TMArticle) 的主键。
每个示例网络服务响应:
文章列表:
[
{
"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"
}
]
您应该几乎总是为每个关系指定逆;在极少数情况下,没有你会过得更好。
看你的数据,似乎给定的部分可以与很多文章相关,所以添加从TMSection
到TMArticle
的一对多关系。
数据只有一篇文章相关的期刊;如果这是普遍正确的,那么您可以添加从 TMJournal
到 TMArticle
.
然后使用以下谓词来获取 TMJournals
:
[journalFR setPredicate:[NSPredicate predicateWithFormat:@"ANY article.sections == %@", self.selectedSectionManagedObjectID];
遗憾的是,FRC 只会观察 Journal
对象的变化,因此您需要专门处理对文章或选择 Section
.