我有一个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模型的头文件:

剧院级:

@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];


更新

电影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"];

10-08 06:27