菜鸟教程小白 发表于 2022-12-11 16:51:34

ios - Realm Swift iOS - 无法设置主键


                                            <p><p>我会尽量简短地解释我的场景,我已经阅读了 Realm GitHub Repo 上关于这个问题的一些评论:</p>

<blockquote>
<p>Terminating app due to uncaught exception &#39;RLMException&#39;, reason:
&#39;Can&#39;t set primary key property &#39;id&#39; to existing value &#39;xxxxxxx&#39;.</p>
</blockquote>

<p>这是我的问题:</p>

<p>我有两个类。</p>

<p><strong>约会模型类</strong></p>

<pre><code>import Foundation
import RealmSwift

class Appointment: Object {

    dynamic var id = 0
    dynamic var user_id: String?
    dynamic var profile_id: String?

    let mainMeeting = List&lt;Meeting&gt;()
    let meetingsWithOtherInfo = List&lt;Meeting&gt;()

    override static func primaryKey() -&gt; String? {
      return &#34;id&#34;
    }
}
</code></pre>

<p><strong> session 模型类</strong></p>

<pre><code>import Foundation
import RealmSwift

class Meeting: Object {

    dynamic var id = 0
    dynamic var name: String?
    dynamic var created_at: String?

    // other info
    dynamic var restaurant_venue: String?

    override static func primaryKey() -&gt; String? {
      return &#34;id&#34;
    }
}
</code></pre>

<p>我正在像这样从我的服务器 API 中获取 <strong>约会</strong></p>

<pre><code>for fetchedAppointment in allAppointmentsFromAlamofire {
    let existingAppointment: Results&lt;(Appointment)&gt;! = realm.objects(Appointment).filter(&#34;id = \(fetchedAppointment[&#34;id&#34;]!)&#34;)

    let newAppointment: Appointment = Appointment()
    newAppointment.id = fetchedAppointment[&#34;id&#34;]! as! Int
    ....

    // add data to Meeting connected to Appointment
    let newMeeting = Meeting()
    newMeeting.id = fetchedAppointment[&#34;meetings&#34;][&#34;id&#34;]! as! Int
    ...

    // update or add new entry
    try! realm.write {
      print(&#34;NEW APPOINTMENT: \(newAppointment)&#34;)
      realm.add(newAppointment, update: existingAppointment.count == 0 ? false : true)
    }
}
</code></pre>

<p>每当程序尝试更新 Realm 中的现有条目时,就会出现错误 - 只要 existingAppointment 为 1。从我从 Github Realm 中读到的解决方法是删除 <strong>override static func primaryKey( )</strong> 在 session 课上。</p>

<p>如果我只是将新条目添加到约会中没有问题,但是如果我要更新,问题就会出现,如果我删除 session 类中的 primaryKey(),问题就会消失 ---- 但是,在我的应用程序的其他屏幕中,我真的需要在 session 类中拥有那个 primaryKey()。</p>

<p>我在这里疯狂的猜测是,每次我需要更新约会中的条目时,我也应该更新 session 。 </p>

<p>所以,问题是:为什么会发生这种情况?我的猜测正确吗?还有什么办法可以解决吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您似乎正在尝试使用不是其主键的值更新 newAppointment 对象。</p>

<pre><code>realm.add(newAppointment, update: existingAppointment.count == 0 ? false : true)
</code></pre>

<p>相反,Realm 希望您提供该对象的 key ,以便它可以更新指定的对象。</p>

<p>您似乎在此处设置键值,这是您应该用于更新的键值。</p>

<pre><code>let newAppointment: Appointment = Appointment()
newAppointment.id = fetchedAppointment[&#34;id&#34;]! as! Int
</code></pre>

<p> <a href="https://realm.io/docs/swift/latest/#updating-objects" rel="noreferrer noopener nofollow">Realm documentation on updating with keys.</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Realm Swift iOS - 无法设置主键,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38091738/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38091738/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Realm Swift iOS - 无法设置主键