在Swift中交换Realm.io数据库中的两个对象 [英] Swap two objects in Realm.io database in Swift

查看:98
本文介绍了在Swift中交换Realm.io数据库中的两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的App中,我将数据存储在Realm数据库中,并在UITableView中显示数据.我在桌上添加了很酷的功能-现在我可以通过拖放来交换单元格了.但是现在我必须在DataBase类中添加方法以交换相关对象.

In my App I store data in Realm database and display data in UITableView. I add cool feature at table - now I can swap cells by drag and drop. But now I have to add method to DataBase class to swap related objects.

我发现Realm是无序的,所以我考虑增加一个属性:index.但是因此,我应该提出有关在数据模型中建立索引项的所有逻辑(在索引处插入,在索引处删除等),在我看来,这是错误的方法.

I've figured out that Realm is unordered, so I thought about adding one more property: index. But consequently I should raise all logic about indexing items in datamodel(insert at index, remove at index, etc), it seems to me wrong approach.

有趣的是有解决这个问题的常用方法吗?

Interesting is there common way to solve such problem?

推荐答案

即使您可以向对象添加索引,我们通常还是建议向架构中添加另一个对象类,该类包含要订购的对象的列表.列表/多对多关系在Realm中是默认排序的,它为您提供了一个方便的界面来移动对象并以其他各种方式修改顺序.

Even though you could add an index to your object, we generally recommend adding another object class to your schema, which carries a list of the objects you want to order. Lists / to-many relations are ordered by default in Realm and offer you a convenient interface to move objects and modify the order in various other ways.

这篇关于在Swift中交换Realm.io数据库中的两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆