核心数据 - 如何验证范围内的属性的唯一性 [英] Core Data - How to validate the uniqueness of an attribute within the scope of

查看:77
本文介绍了核心数据 - 如何验证范围内的属性的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Core Data中实现验证。我想做的是确保属性在相关父对象的范围内是唯一的。换句话说,我想知道如何实现 validates_uniqueness_of:字段,:scope => :parent paradigm(from rails / activerecord)。

I would like to know how to implement a validation in Core Data. What I'd like to do is ensure that an attribute is unique within the scope of a related parent object. In other words, I'm wondering how to implement the validates_uniqueness_of :field, :scope => :parent paradigm (from rails / activerecord) in Core Data.

例如,假设我创建两个模型帖子。每个Post都有一个属性 title 。不同的博客对象可以有相同标题的帖子,但是如何验证博客范围内标题的唯一性?

For example, suppose I create two models - one called Blog and one called Post. Each Post has an attribute called title. Different Blog objects can have Posts with identical titles, but how do I validate the uniqueness of a title within the scope of a Blog?

谢谢!

推荐答案

将关系移动到父级并抓取一组帖子。然后你可以运行一个谓词来检查唯一性,如:

Walk the relationship to the parent and grab the set of posts. Then you can run a predicate against it to check for uniqueness like:

NSSet *set = [[self parent] posts];
NSSet *filtered = [set filteredSetWithPredicate:[NSPredicate preicateWithFormat:@"self != %@ and title == %@", self, [self title]]];
if ([filtered count] > 0) return NO;
return YES;

这篇关于核心数据 - 如何验证范围内的属性的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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