coredata keypath nameOfMovie在实体中找不到< NSSQLEntity Theaters id = 3> [英] coredata keypath nameOfMovie not found in entity <NSSQLEntity Theaters id=3>
问题描述
我有一个coredata项目,我试图进行查询。这是我的coredata模型:
I have a coredata project and I'm trying to make a query. Here is my coredata model:
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];
我收到以下错误:
to-many key not allowed here
to-many key not allowed here
任何人都知道我做错了什么或我的代码缺少什么。非常感谢您的帮助。
By any chance any of you knows what I'm doing wrong or what I'm missing on my code. I'll really appreciate your help.
更新:
我的coredata模型的标头文件:
This are the headers files for my coredata model:
theaters classe:
theaters 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;
电影类:
Movies class:
@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;
计划类别:
Schedules class:
@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
这种问题
一个确切的剧院实体(由您的示例复制)
one exact theater entity (copied by your example)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND SUBQUERY(movies, $mv, $mv.nameOfMovie like %@).@count > 0", _theaterName,_movieOutlet.stringValue];
UPDATE
所有影院运行电影abc
all theaters where movie abc is running
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movies, $mv, $mv.nameOfMovie CONTAINS[c] %@).@count > 0", @"abc"];
btw,您的模型是否正确?
btw, is you model correct?
Theaters <->> Movies = 1:n
有电影
,但电影
只在一个剧院
因此,如果您以名称abc获取 Movie
,您可以获得 Theater
。或者
So every Theater
has x Movies
, but a Movie
is running in only one Theater
So if you fetch a Movie
with name "abc" you can get the Theater
as attibute. Or is it
Theaters <<->> Movies = n:m
?因此电影
和 movieTheaters
是 NSSet< Movie> / NSSet< Theater>
UPDATE 2
仍然需要回答上述问题。什么是正确的关系?是一个电影在x影院或只在一个。你的头文件是如何为ManagedObject类? ;)
still need an answer for the question above. what is the correct relation between? is one movie in x theaters or only in one. how are your header files for the ManagedObject classes? ;)
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"];
这篇关于coredata keypath nameOfMovie在实体中找不到< NSSQLEntity Theaters id = 3>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!