【问题标题】:Core Data to-one relationship returns NULLCore Data 一对一关系返回 NULL
【发布时间】:2015-06-18 20:09:33
【问题描述】:

Batting.team 关​​系并不总是正确保存,有时会返回 null

大约 100 个“团队”NSManagedObjects 被保存并且可以按预期记录它们的属性。然后保存了大约 15000 个“击球”NSManagedObjects,并且所有属性都正确记录,除了关系。

我希望 Batting.team 关​​系指向与 Batting.teamID 具有相同 Team.teamID 值的 Team 对象。

在设置 Batting 关系时,我创建了一个谓词来搜索特定的 Team.teamID,获取请求并假设将该 Team 对象设置为等于我的 Batting.team 关​​系。有时会设置关系属性,但大多数时候没有设置,我无法弄清楚

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    NSError *error;

    NSString* dataPath_TEAMS = [[NSBundle mainBundle] pathForResource:@"teams" ofType:@"json"];
    NSArray* TEAMS = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath_TEAMS] options:kNilOptions error:&error];   

    NSString* dataPath_ABC = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"json"];
    NSArray* BATTING_ABC = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath_ABC] options:kNilOptions error:&error];



    //insert Team objects
    NSFetchRequest *fetchRequestTeams = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityTeams = [NSEntityDescription entityForName:@"Team" inManagedObjectContext:self.managedObjectContext];
    [fetchRequestTeams setEntity:entityTeams];

    for (id t in TEAMS) {
        Team *team = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext];

        team.name       = [t objectForKey:@"name"];
        team.minYearID  = [t objectForKey:@"minYearID"];
        team.maxYearID  = [t objectForKey:@"maxYearID"];
        team.teamID     = [t objectForKey:@"teamID"];

        if (![self.managedObjectContext save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }
    NSArray *fetchedObjectsTeams = [self.managedObjectContext executeFetchRequest:fetchRequestTeams error:&error];
    NSLog(@"Number of records in teams.json - %d", (int)[fetchedObjectsTeams count]);


    //insert Batting objects
    NSFetchRequest *fetchRequestBatting = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityBatting = [NSEntityDescription entityForName:@"Batting" inManagedObjectContext:self.managedObjectContext];
    [fetchRequestBatting setEntity:entityBatting];

    for (id b in BATTING_ABC) {
        Batting *batting = [NSEntityDescription insertNewObjectForEntityForName:@"Batting" inManagedObjectContext:self.managedObjectContext];

        batting.playerID    = [b objectForKey:@"playerID"];
        batting.h           = [b objectForKey:@"h"];
        batting.ab          = [b objectForKey:@"ab"];
        batting.hr          = [b objectForKey:@"hr"];
        batting.rbi         = [b objectForKey:@"rbi"];
        batting.sb          = [b objectForKey:@"sb"];
        batting.r           = [b objectForKey:@"r"];
        batting.bb          = [b objectForKey:@"bb"];
        batting.so          = [b objectForKey:@"so"];
        batting.yearID      = [b objectForKey:@"yearID"];
        batting.teamID      = [b objectForKey:@"teamID"];

        NSPredicate *teamIDPredicate = [NSPredicate predicateWithFormat:@"teamID == %@", [b objectForKey:@"teamID"]];
        [fetchRequestTeams setPredicate:teamIDPredicate];
        NSArray *fetchedSpecificTeam = [self.managedObjectContext executeFetchRequest:fetchRequestTeams error:&error];

        batting.team = [fetchedSpecificTeam firstObject];

        if (![self.managedObjectContext save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
    }

    fetchedObjectsBatting = [self.managedObjectContext executeFetchRequest:fetchRequestBatting error:&error];
    for (Batting *b in fetchedObjectsBatting) {
        NSLog(@"playerID:   %@", b.playerID);
        NSLog(@"yearID:     %@", b.yearID);
        NSLog(@"teamID:     %@", b.teamID);
        NSLog(@"team:       %@\n\n", b.team.name);
    }

    return YES;
}

【问题讨论】:

    标签: ios core-data nspredicate nsmanagedobject relationships


    【解决方案1】:

    从您的模型的屏幕截图中,它被配置为每个Team 只能有一个Batting。每次将batting.team 设置为给定的Team 时,CoreData 都会删除从该Team 到其当前 Batting 对象的链接(从而将Batting 对象的team 设置为零)。

    您应该在模型编辑器中使用右侧的数据模型检查器将Team 实体的batting 关系定义为“对多”(每个Team 可以有多个Battings):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2012-11-26
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多