首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >唯一性约束特性需要ios部署目标9.0或更高的核心数据

唯一性约束特性需要ios部署目标9.0或更高的核心数据
EN

Stack Overflow用户
提问于 2015-10-13 10:27:00
回答 1查看 659关注 0票数 1

IOS 9有一个有用的特性,可以在属性上添加唯一的约束。IOS 9唯一约束

但是,我希望向上支持IOS 8,除非我将部署目标设置为9,否则无法编译。

是否有一种方法可以创建两个数据模型,并在编译器指令下使用数据模型AIOS 8以及数据模型BIOS 9以及更高版本?

-更新

下面的代码用于手动添加唯一约束。

代码语言:javascript
复制
- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CL" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        NSArray *entities = [_managedObjectModel entities];
        NSEntityDescription *entity = [entities firstObject];
        NSArray *properties = @[@"serverFileId"];
        [entity setUniquenessConstraints:@[properties]];
    }
    return _managedObjectModel;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-13 11:16:17

这是个很有趣的问题瑞安。我想到的唯一解决办法是:

  • 您可以创建一个符合iOS8+iOS9的模型(w/o唯一性约束)。
  • 使用以下方法保护持久性存储协调器创建代码的某些部分

if #available(iOS 9.0, *) { // add uniqueness constraints here }

  • 您可以像描述的这里一样,以编程的方式在受保护的作用域中添加唯一性约束。

更新:--我一直在寻找一种方法,以编程方式将唯一性约束应用于属性,并最终得到了CoreData.NSEntityDescription的类定义。

代码语言:javascript
复制
/* Returns/sets uniqueness constraints for the entity. A uniqueness constraint is a set of one or more attributes whose value must be unique over the set of instances of that entity.
    Returns/sets an array of arrays, each of which contains one or more NSAttributeDescription or NSString instances (strings must be the names of attributes on the entity) on which the constraint is registered. 
    This value forms part of the entity's version hash. Stores which do not support uniqueness constraints should refuse to initialize when given a model containing such constraints.
    Discussion: uniqueness constraint violations can be computationally expensive to handle. It is highly suggested that there be only one uniqueness constraint per entity hierarchy,
    although subentites may extend a sueprentity's constraint.
*/

@available(iOS 9.0, *)
public var uniquenessConstraints: [[AnyObject]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33100285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档