iOS>> CoreData>>我应该创建ID属性还是可以使用SQLite对象ID? [英] iOS >> CoreData >> Should I Create an ID Attribute or Can I Use the SQLite Object ID?

查看:65
本文介绍了iOS>> CoreData>>我应该创建ID属性还是可以使用SQLite对象ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CoreData时,我是否需要创建一个ID属性以为每条记录具有某种唯一键,还是可以访问表中每行的数据库ID字段?

When working with CoreData, do I need to create an ID attribute to have some sort of unique key for each record or can I access the data base ID field per each line in the table?

如果有一种方法可以使用DB ID字段,那么当通过managedObject实例保存记录时,我将对如何使用它有一些了解...

If there's a way to use the DB ID field, I would appreciate some direction about how to get to it when saving the record via the managedObject instance...

推荐答案

NSManagedObjects具有对象ID属性。但是,当您创建ManagedObject时,将有一个临时ID,将对象存储到数据库中后,它将获得永久ID。以我的经验,最好为每个数据创建自己的ID。例如,如果它是简单表,则可以简单地将时间戳记用作唯一ID;或者,如果它是具有实时更新和频繁更新的复杂表,则应使用带有时间戳的表中的某些字段组合(可以将值转换为base64使其成为真实ID;))。

NSManagedObjects has a object id property. But when you create an ManagedObject there will be a temporary id, after storing the object into the DB it will get the permanent id. In my experience it is better to create your own id for each data. For example, if it is a simple table you can simply use timestamp as unique id or if it is a complex table with live and frequent update you should use some combination of fields in the table with the time stamp (you can convert the value to base64 to make it as a real id ;) ).

这篇关于iOS>> CoreData>>我应该创建ID属性还是可以使用SQLite对象ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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