简体   繁体   English

Objective-C:领域中对象的部分更新

[英]Objective-C: Partial update of object in realm

The problem: I'm using Realm along the AFNetworking library. 问题:我正在AFNetworking库中使用Realm。 To keep things clean I'm working with custom serializers parsing incoming json responses and mapping them to objects. 为了保持整洁,我正在使用自定义序列化程序来解析传入的json响应并将它们映射到对象。 The returned objects a than saved, updated or in some cases forwarded. 返回的对象比保存,更新或在某些情况下转发。 The problem is that some relations get overwritten and I need to execute a kind of partial update. 问题是某些关系被覆盖,我需要执行一种部分更新。

Is there a way in realm to copy a persisted object and add its values to a non-persisted object, updated with response values only? 领域中是否有一种方法可以复制持久对象并将其值添加到仅使用响应值进行更新的非持久对象?

Example: 例:

Current state: 当前状态:

cat{
   name: "kitty"
   owners: ["peter"] 
}

Incoming Response: 传入响应:

cat{
    name:"pussy"
    owner:[]
}

Wanted Result: 想要的结果:

cat{
    name:"pussy"
    owner:["peter"]
}

We JUST updated that section of the Realm documentation to clarify this behaviour. 我们只是更新了Realm文档的这一部分,以阐明这种行为。

Unfortunately, because 'nil' is also a valid value for every optional property of a Realm object, it's not possible for Realm to intelligently tell the difference between when an empty value supplied should be ignored, or should be used to empty the property it's pointing at. 不幸的是,由于nil也是Realm对象的每个可选属性的有效值,因此Realm不可能明智地区分何时应该忽略提供的空值,​​或者应该使用该值来清空其指向的属性之间的区别。在。

The easiest way would be to use the JSON you've received to build an NSDictionary that only contains the value you wish to update, and then pass that to Realm through the +[RLMObject createOrUpdateInRealm:withValue:] to explicitly only update the properties on that object that you've supplied. 最简单的方法是使用收到的JSON来构建一个NSDictionary ,该NSDictionary仅包含要更新的值,然后通过+[RLMObject createOrUpdateInRealm:withValue:]将其传递给Realm以显式仅更新其属性。您提供的那个对象。

If you want to update your object using a RLMObject copy of that object, you'll need to make sure the copy contains all of the properties you want to keep. 如果要使用该对象的RLMObject副本更新对象,则需要确保该副本包含要保留的所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM