菜鸟教程小白 发表于 2022-12-12 21:52:21

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>&lt;NSFetchRequest: 0x60b000148a40&gt; (entity: treatmentVisits; predicate: (ANY treatmentVisits == {&lt;Visits: 0x60b0000a0ad0&gt; (entity: VisitsEntity; id: 0xd0000000000c000a &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p3&gt; ; data: {
visitCause = &#34;&#34;;
visitEndDate = nil;
visitId = nil;
visitRecommendation = &#34;&#34;;
visitStartDate = &#34;2016-08-11 04:43:50 +0000&#34;;
visitTreatment = &#34;&lt;relationship fault: 0x60300025e6d0 &#39;visitTreatment&#39;&gt;&#34;;
visitType = 0;
visitUser = &#34;0xd000000001940000 &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101&gt;&#34;;
}), &lt;Visits: 0x60b0000bec10&gt; (entity: VisitsEntity; id: 0xd00000000008000a &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p2&gt; ; data: {
visitCause = &#34;&#34;;
visitEndDate = nil;
visitId = nil;
visitRecommendation = &#34;&#34;;
visitStartDate = &#34;2016-08-04 20:26:33 +0000&#34;;
visitTreatment = &#34;&lt;relationship fault: 0x60300025e850 &#39;visitTreatment&#39;&gt;&#34;;
visitType = 0;
visitUser = &#34;0xd000000001940000 &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/UsersEntity/p101&gt;&#34;;
}), &lt;Visits: 0x60b000148570&gt; (entity: VisitsEntity; id: 0xd00000000010000a &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p4&gt; ; data: &lt;fault&gt;), &lt;Visits: 0x60b000148620&gt; (entity: VisitsEntity; id: 0xd00000000004000a &lt;x-coredata://5CDB188C-2177-415B-9B74-E30B20EA4DAA/VisitsEntity/p1&gt; ; data: &lt;fault&gt;)}.visitTreatment); sortDescriptors: ((
&#34;(treatmentStartTime, descending, compare:)&#34;
)); 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:@&#34;TreatmentsEntity&#34;];
</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(@&#34;Unresolved error %@, %@&#34;, error, );
    abort();   
} else {
    NSLog(@&#34;Results is %@&#34;,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(@&#34;Unresolved error %@, %@&#34;, error, );
    abort();
} else {
    NSLog(@&#34;Results %@&#34;,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]
查看完整版本: ios - NSFetchResultsController 的 NSPredicate