菜鸟教程小白 发表于 2022-12-12 11:01:43

ios - executeFetchRequest 在空核心数据上返回 nil


                                            <p><p>我在 Core Data 中遇到了一个奇怪的问题。基本上,我有一个空的核心数据,其中包含学生和教师等几个模型。我尝试执行以下代码:</p>

<pre><code>NSFetchRequest *request = ;
request.predicate = ;

// Determines if there is a match for student
NSError *error;
NSArray *matches = ;
if (error || !matches || &gt; 1) {
    NSLog(@&#34;Error in retrieving login match for student&#34;);
}
else if () {
    student = ;
}
</code></pre>

<p>奇怪的是 <code>matches</code> 结果是 <code>nil</code> 并且错误消息被打印出来。匹配不应该是一个空数组,因为一个空的核心数据只是意味着上下文找不到满足这些条件的 NSManagedObject 吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>简短回答 = 是的,应该。</p>

<p>根据<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/executeFetchRequest:error:" rel="noreferrer noopener nofollow">Apple doc</a> :</p>

<blockquote>
<p>Return Value of executeFetchRequest ... An array of objects that meet the criteria specified by request
fetched from the receiver and from the persistent stores associated
with the receiver’s persistent store coordinator. <strong>If an error occurs</strong>,
<strong>returns nil</strong>. <strong>If no objects</strong> match the criteria specified by request,
returns an <strong>empty array</strong>.</p>
</blockquote>

<p>因此,请检查您的 NSError</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - executeFetchRequest 在空核心数据上返回 nil,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/25174139/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/25174139/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - executeFetchRequest 在空核心数据上返回 nil