coredata keypath nameOfMovie找不到实体<NSSQLEntity Theaters id = 3>

我有一个coredata项目,我试图做一个查询。 这是我的coredata模型:

在这里输入图像说明

NSManagedObjectContext *moc = [self managedObjectContext]; NSEntityDescription *theaterDescription = [ NSEntityDescription entityForName:@"Theaters" inManagedObjectContext:moc]; NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND nameOfMovie like %@",_theaterName,_movieOutlet.stringValue]; NSFetchRequest *theaterRequestTwo = [NSFetchRequest new]; theaterRequestTwo.entity = theaterDescription; theaterRequestTwo.predicate = theaterPredicate; NSError *error = nil; NSArray *theaterResults = [moc executeFetchRequest:theaterRequestTwo error:&error]; 

但是我得到这个错误:

 keypath nameOfMovie not found in entity <NSSQLEntity Theaters id=3> 

我也试过:

 NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND Movies.nameOfMovie like %@",_theaterName,_movieOutlet.stringValue]; 

但是我得到这个错误:

 keypath Movies.nameOfMovie not found in entity <NSSQLEntity Theaters id=3> 

我也试过:

 NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND movies.nameOfMovie like %@",_theaterName,_movieOutlet.stringValue]; 

我得到了以下错误:

一对多的钥匙不允许在这里

任何一个机会,你都知道我在做什么错,或者我的代码中缺less什么。 我会很感激你的帮助。

更新:

这是我的coredata模型的头文件:

剧院类别:

 @class Movies; @interface Theaters : NSManagedObject @property (nonatomic, retain) NSString * nameOfTheater; @property (nonatomic, retain) NSSet *movies; @end @interface Theaters (CoreDataGeneratedAccessors) - (void)addMoviesObject:(Movies *)value; - (void)removeMoviesObject:(Movies *)value; - (void)addMovies:(NSSet *)values; - (void)removeMovies:(NSSet *)values; 

电影课:

 @class Schedules, Theaters; @interface Movies : NSManagedObject @property (nonatomic, retain) NSString * nameOfMovie; @property (nonatomic, retain) NSSet *showTimes; @property (nonatomic, retain) Theaters *theaters; @end @interface Movies (CoreDataGeneratedAccessors) - (void)addShowTimesObject:(Schedules *)value; - (void)removeShowTimesObject:(Schedules *)value; - (void)addShowTimes:(NSSet *)values; - (void)removeShowTimes:(NSSet *)values; 

课程表:

 @interface Schedules : NSManagedObject @property (nonatomic, retain) NSDate * showTimes; @property (nonatomic, retain) NSSet *movie; @end @interface Schedules (CoreDataGeneratedAccessors) - (void)addMovieObject:(Movies *)value; - (void)removeMovieObject:(Movies *)value; - (void)addMovie:(NSSet *)values; - (void)removeMovie:(NSSet *)values; 

您可以使用SUBQUERY来解决这类问题

一个确切的戏剧实体(由你的例子复制)

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND SUBQUERY(movies, $mv, $mv.nameOfMovie like %@).@count > 0", _theaterName,_movieOutlet.stringValue]; 

UPDATE

电影ABC正在运行的所有影院

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movies, $mv, $mv.nameOfMovie CONTAINS[c] %@).@count > 0", @"abc"]; 

顺便说一句,你是模型是正确的?

 Theaters <->> Movies = 1:n 

所以每个Theater都有x部Movies ,但是Movie只在一个Theater运行。所以如果你用名字“abc”来获取一部Movie ,你可以把Theater作为属性。 或者是

 Theaters <<->> Movies = n:m 

? 所以moviesmovieTheatersNSSet<Movie>/NSSet<Theater>

更新2

上述问题仍然需要回答。 什么是正确的关系? 是在x个影院的一部电影或只在一个。 ManagedObject类的头文件怎么样? ;)

 NSEntityDescription *schedulesDescription = [NSEntityDescription entityForName:@"Schedules" inManagedObjectContext:moc]; // something like this if 1:n. Try and post logs, have no IDE at the moment NSPredicate *predicate = [NSPredicate predicateWithFormat:@"movie.nameOfMovie == %@ AND movie.movieTheaters.nameOfTheater == %@", @"movie8", @"theaterOne"]; // if n:m, as far as I remember you couldn't fetch over 2 n:m relations via . NSPredicate *predicate = [NSPredicate predicateWithFormat:@"movie.nameOfMovie == %@ AND SUBQUERY(movie, $mv, $mv.movieTheaters.nameOfTheater == %@).@count > 0", @"movie8", @"theaterOne"];