Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoreData 一对多关系 #150

Closed
dmelon opened this issue Jul 29, 2015 · 4 comments
Closed

CoreData 一对多关系 #150

dmelon opened this issue Jul 29, 2015 · 4 comments

Comments

@dmelon
Copy link

dmelon commented Jul 29, 2015

CoreData中的“一对多”的关系使用NSSet而不是NSArray。这样的话,一个NSManagedObject中的关系属性(NSSet *类型),就没法利用objectArrayWithKeyValuesArray来自动生成对象了。请问有什么解决方法吗?

@dmelon dmelon closed this as completed Jul 29, 2015
@dmelon dmelon reopened this Jul 29, 2015
@chengleyi
Copy link

我也有这样的问题,请问如何解决。。。

@jaysonjh
Copy link

老师与学生一对多关系

NSDictionary *techerAndStudents = @{
                                        @"uid":[[NSUUID UUID] UUIDString],
                                        @"name":@"jayson",
                                        @"students":@[
                                                @{
                                                    @"uid":[[NSUUID UUID] UUIDString],
                                                    @"name":@"11",
                                                    },
                                                @{
                                                    @"uid":[[NSUUID UUID] UUIDString],
                                                    @"name":@"12",
                                                    }
                                                ],
                                        };

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        [Teacher setupObjectClassInArray:^NSDictionary *{
            return @{@"students":[Student class]};
        }];
        Teacher *teacher = [Teacher objectWithKeyValues:techerAndStudents context:localContext];
        //此处students为nil,还得加入以下才可实现coredata的一对多。
        Student *s1 = [Student objectWithKeyValues:@{@"uid":[[NSUUID UUID] UUIDString],@"name":@"11",} context:localContext];
        Student *s2 = [Student objectWithKeyValues:@{@"uid":[[NSUUID UUID] UUIDString],@"name":@"12",} context:localContext];
        [teacher addStudentsObject:s1];
        [teacher addStudentsObject:s2];
        NSLog(@"Teacher:%@,%@,%@",teacher.name,teacher.uid,teacher.students);
    }];
NSDictionary *techerAndStudents = @{
                                        @"uid":[[NSUUID UUID] UUIDString],
                                        @"name":@"jayson",
                                        @"students":@[
                                                @{
                                                    @"uid":[[NSUUID UUID] UUIDString],
                                                    @"name":@"11",
                                                    },
                                                @{
                                                    @"uid":[[NSUUID UUID] UUIDString],
                                                    @"name":@"12",
                                                    }
                                                ],
                                        };

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        [Teacher setupObjectClassInArray:^NSDictionary *{
            return @{@"students":[Student class]};
        }];
        Teacher *teacher = [Teacher objectWithKeyValues:techerAndStudents context:localContext];
        Student *s1 = [Student objectWithKeyValues:@{@"uid":[[NSUUID UUID] UUIDString],@"name":@"11",} context:localContext];
        Student *s2 = [Student objectWithKeyValues:@{@"uid":[[NSUUID UUID] UUIDString],@"name":@"12",} context:localContext];
        [teacher addStudentsObject:s1];
        [teacher addStudentsObject:s2];
        NSLog(@"Teacher:%@,%@,%@",teacher.name,teacher.uid,teacher.students);
    }];

跟踪代码在

// 2.如果是模型属性
            MJPropertyType *type = property.type;
            Class typeClass = type.typeClass;
            Class objectClass = [property objectClassInArrayForClass:[self class]];
           //fromFoundation为NO,typeClass为NSSet 结果就无法继续再对NSSet中的对象进行映射了
            if (!type.isFromFoundation && typeClass) {
                value = [typeClass objectWithKeyValues:value context:context error:error];
            }

@CoderMJLee 有更好的方式去解决一对多的问题吗?

@yuhanle
Copy link

yuhanle commented Jul 7, 2016

@jaysonjh 像你这种写法可以达到目的吗?
我试了试,感觉有些问题

@wolfcon
Copy link
Collaborator

wolfcon commented Sep 8, 2021

Duplicate of #43

@wolfcon wolfcon marked this as a duplicate of #43 Sep 8, 2021
@wolfcon wolfcon closed this as completed Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants