一对多对多关系查询中的核心数据
core data in one to many to many relation query
我有一个这样的核心数据对象:
我构建了一个谓词来获取计划时间和每个选择时间我需要获取电影和剧院
凭借档期的价值,我试图获得剧院
这是我的代码:
NSString *nameOfMovie = [[scheduleResults movie] valueForKey:@"nameOfMovie"];
NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY (showTimes, $x, $x.nameOfMovie == %@ AND $x.showTimes.showTimes == %@).@count > 0", nameOfMovie, showTimes];
但是我的谓词出现了这个错误:
此处不允许对多密钥
你们中有人知道我的谓词做错了什么吗?
我发现这个问题很有趣,所以用 swift 创建了一个简单的游乐场来测试你的案例的子查询。我认为你在你的数据模型中遗漏了一些关系,如果你真的关心它,你应该用单数命名实体,如电影、日程、剧院等,这更有意义,因为实体总是单数,关联可以是单数或复数取决于一对多或一对一关系。
这是我想出的数据模型,
// a Movie can have multiple schedule and a schedule can have multiple movies at the same time
Schedule << ------- >> Movie
// a Movie can be running in multiple theater and a theater can have multiple movies
Movie << ------>> Theater
所以,基于上面的关系,我创建了一个游乐场文件来创建一些夹具数据并进行游戏,
class Schedule: NSObject {
var showTime: NSDate!
var movies: [Movie]!
}
class Theater: NSObject {
var name: String!
var movies: [Movie]!
}
class Movie: NSObject {
var nameOfMovie: String!
var theaters: [Theater]!
var showTimes: [Schedule]!
}
let hitech = Theater()
hitech.name = "Hitech Cinema"
let kino = Theater()
kino.name = "Kino"
let warner = Theater()
warner.name = "Warners"
let interstellar = Movie()
interstellar.nameOfMovie = "Interstellar"
let gravity = Movie()
gravity.nameOfMovie = "Gravity"
let wrathOfSpace = Movie()
wrathOfSpace.nameOfMovie = "Wrath of Space"
let today = NSDate()
let yesterday = today.dateByAddingTimeInterval(-60 * 60 * 24)
let tomorrow = today.dateByAddingTimeInterval(60 * 60 * 24)
let todaySchedule = Schedule()
todaySchedule.showTime = today
let yesterdaySchedule = Schedule()
yesterdaySchedule.showTime = yesterday
let tomorrowSchedule = Schedule()
tomorrowSchedule.showTime = tomorrow
todaySchedule.movies = [interstellar, gravity]
tomorrowSchedule.movies = [interstellar, wrathOfSpace]
yesterdaySchedule.movies = [wrathOfSpace, gravity]
interstellar.showTimes = [todaySchedule, tomorrowSchedule]
gravity.showTimes = [todaySchedule, yesterdaySchedule]
wrathOfSpace.showTimes = [tomorrowSchedule, yesterdaySchedule]
interstellar.theaters = [kino, hitech]
gravity.theaters = [kino, warner]
wrathOfSpace.theaters = [warner, hitech]
kino.movies = [interstellar, gravity]
hitech.movies = [interstellar, wrathOfSpace]
warner.movies = [wrathOfSpace, gravity]
let theaters:NSArray = [hitech, warner, kino]
var nameOfMovie = "Gravity"
var date = yesterday
var predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)
let threatersYesterdayGravity = theaters.filteredArrayUsingPredicate(predicate)
nameOfMovie = "Interstellar"
date = tomorrow
predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)
let threatersTomorrowInterstellar = theaters.filteredArrayUsingPredicate(predicate)
结果如我所料。您还可以使用上述关系和子查询来满足您的需要。
我有一个这样的核心数据对象:
我构建了一个谓词来获取计划时间和每个选择时间我需要获取电影和剧院
凭借档期的价值,我试图获得剧院
这是我的代码:
NSString *nameOfMovie = [[scheduleResults movie] valueForKey:@"nameOfMovie"];
NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY (showTimes, $x, $x.nameOfMovie == %@ AND $x.showTimes.showTimes == %@).@count > 0", nameOfMovie, showTimes];
但是我的谓词出现了这个错误:
此处不允许对多密钥
你们中有人知道我的谓词做错了什么吗?
我发现这个问题很有趣,所以用 swift 创建了一个简单的游乐场来测试你的案例的子查询。我认为你在你的数据模型中遗漏了一些关系,如果你真的关心它,你应该用单数命名实体,如电影、日程、剧院等,这更有意义,因为实体总是单数,关联可以是单数或复数取决于一对多或一对一关系。
这是我想出的数据模型,
// a Movie can have multiple schedule and a schedule can have multiple movies at the same time
Schedule << ------- >> Movie
// a Movie can be running in multiple theater and a theater can have multiple movies
Movie << ------>> Theater
所以,基于上面的关系,我创建了一个游乐场文件来创建一些夹具数据并进行游戏,
class Schedule: NSObject {
var showTime: NSDate!
var movies: [Movie]!
}
class Theater: NSObject {
var name: String!
var movies: [Movie]!
}
class Movie: NSObject {
var nameOfMovie: String!
var theaters: [Theater]!
var showTimes: [Schedule]!
}
let hitech = Theater()
hitech.name = "Hitech Cinema"
let kino = Theater()
kino.name = "Kino"
let warner = Theater()
warner.name = "Warners"
let interstellar = Movie()
interstellar.nameOfMovie = "Interstellar"
let gravity = Movie()
gravity.nameOfMovie = "Gravity"
let wrathOfSpace = Movie()
wrathOfSpace.nameOfMovie = "Wrath of Space"
let today = NSDate()
let yesterday = today.dateByAddingTimeInterval(-60 * 60 * 24)
let tomorrow = today.dateByAddingTimeInterval(60 * 60 * 24)
let todaySchedule = Schedule()
todaySchedule.showTime = today
let yesterdaySchedule = Schedule()
yesterdaySchedule.showTime = yesterday
let tomorrowSchedule = Schedule()
tomorrowSchedule.showTime = tomorrow
todaySchedule.movies = [interstellar, gravity]
tomorrowSchedule.movies = [interstellar, wrathOfSpace]
yesterdaySchedule.movies = [wrathOfSpace, gravity]
interstellar.showTimes = [todaySchedule, tomorrowSchedule]
gravity.showTimes = [todaySchedule, yesterdaySchedule]
wrathOfSpace.showTimes = [tomorrowSchedule, yesterdaySchedule]
interstellar.theaters = [kino, hitech]
gravity.theaters = [kino, warner]
wrathOfSpace.theaters = [warner, hitech]
kino.movies = [interstellar, gravity]
hitech.movies = [interstellar, wrathOfSpace]
warner.movies = [wrathOfSpace, gravity]
let theaters:NSArray = [hitech, warner, kino]
var nameOfMovie = "Gravity"
var date = yesterday
var predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)
let threatersYesterdayGravity = theaters.filteredArrayUsingPredicate(predicate)
nameOfMovie = "Interstellar"
date = tomorrow
predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)
let threatersTomorrowInterstellar = theaters.filteredArrayUsingPredicate(predicate)
结果如我所料。您还可以使用上述关系和子查询来满足您的需要。