我有一个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];
我收到以下错误:
这里不允许使用多个键
任何人都知道我做错了什么或者我的代码中缺少什么。我非常感谢你的帮助。
更新
这是我的coredata模型的头文件:
剧院classe:
@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;
答案 0 :(得分:4)
您可以使用SUBQUERY
来解决此类问题
一个精确的剧院实体(由你的例子复制)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND SUBQUERY(movies, $mv, $mv.nameOfMovie like %@).@count > 0", _theaterName,_movieOutlet.stringValue];
<强>更新强>
电影abc正在运行的所有影院
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movies, $mv, $mv.nameOfMovie CONTAINS[c] %@).@count > 0", @"abc"];
顺便问一下,你的模特是正确的吗?
Theaters <->> Movies = 1:n
所以每个Theater
都有x Movies
,但Movie
只在一个Theater
中运行
所以如果你拿一个名字为Movie
&#34; abc&#34;你可以得到Theater
作为attibute。或者是
Theaters <<->> Movies = n:m
?因此movies
和movieTheaters
为NSSet<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"];