Batting.team关系并不总是正确保存并且有时会返回null
大约100' Team' NSManagedObjects已保存,可以按预期记录其属性。然后大约15000'击球'保存NSManagedObjects,除关系外,所有属性都正确记录。
我希望Batting.team关系指向Team对象,该对象具有与Batting.teamID相同的Team.teamID值。
设置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;
}
答案 0 :(得分:3)
从模型的屏幕截图中,对其进行配置,以便每个Team
只能有一个Batting
。每次将batting.team
设置为给定Team
时,CoreData都会删除从Team
到其当前Batting对象的链接(从而设置Batting
对象的team
没有。
您应该使用右侧的数据模型检查器将Team
实体的batting
关系定义为“to-Many”(每个Team
可以有多个Battings
)在模型编辑器中的手边: