iOS Core Data 如何正确初始化实体关系?

发布于 2024-12-12 18:05:09 字数 1640 浏览 0 评论 0原文

我的核心数据模型中有一对多关系。我需要创建一个新实体并保存它。该实体具有一对多关系,生成以下代码:

- (void)addRelationshipEvent1:(NSSet *)values;
- (void)removeRelationshipEvent1:(NSSet *)values;

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        ApplicationRecord *newManagedObject = (ApplicationRecord*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];


        newManagedObject.startDate = [NSDate date];
        newManagedObject.stopDate = [[NSDate date] dateByAddingTimeInterval:120];

//继续添加单独的动态属性

最初将-toMany关系集设置为nil是否正确?或者我是否需要在此处初始化一个(空?)集并分配它?如果我将初始集设置为零,我以后可以添加额外的对象吗?

        newManagedObject.relationshipEvent1  = nil;
        newManagedObject.relationshipEvent2 = nil;
    //...

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();

        }

I have a one to many relationship in my core data model. I need to create a new entity and save it. The entity has a one to many relationship which generated the following code:

- (void)addRelationshipEvent1:(NSSet *)values;
- (void)removeRelationshipEvent1:(NSSet *)values;

.

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        ApplicationRecord *newManagedObject = (ApplicationRecord*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];


        newManagedObject.startDate = [NSDate date];
        newManagedObject.stopDate = [[NSDate date] dateByAddingTimeInterval:120];

//keep adding individual dynamic properties

is it correct to set the -toMany relationship sets to nil initially? Or do I need to initialize an (empty?) set here and assign it? Would I be able to add extra objects later if I set the initial set to nil?

        newManagedObject.relationshipEvent1  = nil;
        newManagedObject.relationshipEvent2 = nil;
    //...

        // Save the context.
        NSError *error = nil;
        if (![context save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2024-12-19 18:05:09

亚历克斯,

你不需要初始化你的关系。只需使用提供的访问器或辅助函数,Core Data 就会处理它。 IOW,只有当您需要实际使用它时才担心属性/关系。

安德鲁

Alex,

You don't need to intialize your relationships. Just use the supplied accessors or helper functions and Core Data takes care of it. IOW, only worry about the property/relationship when you need to actually use it.

Andrew

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文