ios - NSFetchResultsController 的 NSPredicate
<p><p>我正在尝试为我在 <code>UITableView</code> 中使用的 <code>NSFetchResultsController</code> 创建一个 <code>NSPredicate</code>。
我有 3 个实体,<code>User</code>(一对多)-> <code>Visit</code>(一对多)-> <code>Treatment</code>,并希望获得所有用户接受的治疗。</p>
<p>我尝试过使用子查询、<code>SELF</code>、<code>ANY</code>,并且只返回 <code>Visits</code> 而不是 <code>Treatments</code>。</p>
<p>有人可以指导我如何从用户实体访问治疗实体吗?</p>
<p>提前感谢您的帮助!</p>
<p> <a href="http://i.stack.imgur.com/KZpSf.png" rel="noreferrer noopener nofollow">Image of the CoreData Model</a> </p>
<p><strong>编辑</strong></p>
<p>这是我目前用来尝试获取治疗的代码</p>
<pre><code>NSFetchRequest *fetchRequest = [ initWithEntityName:kTreatmentVisits];
fetchRequest.sortDescriptors = descriptors;
fetchRequest.fetchBatchSize = 10;
fetchRequest.predicate = ;
</code></pre>
<p>结果我得到的是访问,而不是治疗</p>
<pre><code><NSFetchRequest: 0x60b000148a40> (entity: treatmentVisits; predicate: (ANY treatmentVisits == {<Visits: 0x60b0000a0ad0> (entity: VisitsEntity; id: 0xd0000000000c000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p3> ; data: {
visitCause = "";
visitEndDate = nil;
visitId = nil;
visitRecommendation = "";
visitStartDate = "2016-08-11 04:43:50 +0000";
visitTreatment = "<relationship fault: 0x60300025e6d0 'visitTreatment'>";
visitType = 0;
visitUser = "0xd000000001940000 <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101>";
}), <Visits: 0x60b0000bec10> (entity: VisitsEntity; id: 0xd00000000008000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p2> ; data: {
visitCause = "";
visitEndDate = nil;
visitId = nil;
visitRecommendation = "";
visitStartDate = "2016-08-04 20:26:33 +0000";
visitTreatment = "<relationship fault: 0x60300025e850 'visitTreatment'>";
visitType = 0;
visitUser = "0xd000000001940000 <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101>";
}), <Visits: 0x60b000148570> (entity: VisitsEntity; id: 0xd00000000010000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p4> ; data: <fault>), <Visits: 0x60b000148620> (entity: VisitsEntity; id: 0xd00000000004000a <x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p1> ; data: <fault>)}.visitTreatment); sortDescriptors: ((
"(treatmentStartTime, descending, compare:)"
)); batch size: 10; type: NSManagedObjectResultType; )
</code></pre>
<p>我希望能够访问 <strong>visitTreatment</strong></p> 中的内容
<p>我也试过</p>
<pre><code>fetchRequest.predicate = ;
</code></pre>
<p>结果相同。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,如果您想获取治疗,您的 FRC 的基础提取必须指定治疗实体:</p>
<pre><code>NSFetchRequest *fetchRequest = [ initWithEntityName:@"TreatmentsEntity"];
</code></pre>
<p>然后通过指定谓词将结果限制为给定 <code>UsersEntity</code> 的处理,例如 <code>requiredUser</code>:</p>
<pre><code>fetchRequest.predicate = ;
</code></pre>
<p>然后您需要直接执行获取请求:</p>
<pre><code>NSError *error = nil;
NSArray *results = ;
if (results == nil) {
NSLog(@"Unresolved error %@, %@", error, );
abort();
} else {
NSLog(@"Results is %@",results);
}
</code></pre>
<p>或者,对于获取的结果 Controller ,您使用 fetchRequest 初始化 FRC,然后调用 performFetch:</p>
<pre><code>NSFetchedResultsController *frc = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
NSError *error = nil;
if (!) {
NSLog(@"Unresolved error %@, %@", error, );
abort();
} else {
NSLog(@"Results %@",frc.fetchedObjects);
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - NSFetchResultsController 的 NSPredicate,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39173965/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39173965/
</a>
</p>
页:
[1]