本文介绍了核心数据主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能看起来很蠢,但我还是不知道如何在xcdatamodel文件中将属性标记为主键。
我的持久存储是sqlite文件。
任何人都可以帮助我?
This may seem stupid, but I still couldn't figure out how to mark a attribute as a primary key in the xcdatamodel file.My persistent storage is sqlite file.Can anyone help me?
在这种情况下,如何验证ID是唯一的?
我应该写一个验证方法还是什么?
In that case, how can I "validate" a ID to be unique?Should I write a validation method or something?
推荐答案
您的选择是:
- 使用
- [NSManagedObject objectID]
。请注意,此ID是暂时的,直到第一次保存对象或调用- [NSManagedObjectContext getsPermanentIDsForObjects:error:]
- 使用
CFUUID
系列函数为-awakeFromInsert
方法中的每个对象生成UUID - 创建自己的主键类系统,在模型中存储整数,并通过创建每个对象来增加它。
- Use
-[NSManagedObject objectID]
. Note that this ID is temporary until either the object is saved for the first time or you call-[NSManagedObjectContext obtainPermanentIDsForObjects:error:]
- Use the
CFUUID
family of functions to generate a UUID for each object in your-awakeFromInsert
method - Create your own primary key-like system that stores an integer in your model and increments it with the creation of each object
没有什么好的方法来验证属性是否是唯一的。您最接近的是确保它在创建时是唯一的,然后实现一个自定义setter方法,阻止任何人更改ID。
There is no good way to validate that a property is unique. The closest you'll get is to ensure it is unique at creation time, and then implement a custom setter method that stops anyone ever changing the ID.
这篇关于核心数据主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!