核心数据关系中对象的谓词
Posted
技术标签:
【中文标题】核心数据关系中对象的谓词【英文标题】:Predicate for objects in core data relationships 【发布时间】:2014-07-05 21:14:15 【问题描述】:我最近问了一个类似的问题,但被引导到一个替代解决方案,但现在我发现我在另一个领域确实需要这个。我的应用程序会跟踪参加锦标赛的玩家。每个玩家都会签到,然后将他们的比赛添加到数据库中。每个玩家可以在多天签到,因此存在一对多的关系。每场比赛可以有两个球员,球员可以有多个比赛,所以球员和比赛是多对多的关系。
我有三个实体:Player、CheckIn 和 Match
玩家实体 > 签入实体 玩家实体 > 匹配实体
播放器.h:
@class CheckIn, Match;
@interface Player : NSManagedObject
@property (nonatomic, retain) NSDate * birthDay;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSString * playerID;
@property (nonatomic, retain) NSString * playerImage;
@property (nonatomic, retain) NSString * playerImageSmall;
@property (nonatomic, retain) NSDate * timeOfLastCheckIn;
@property (nonatomic, retain) NSNumber * isCheckedIn;
@property (nonatomic, retain) NSOrderedSet *playerCheckIns;
@property (nonatomic, retain) NSSet *playerMatches;
@end
@interface Player (CoreDataGeneratedAccessors)
- (void)insertObject:(CheckIn *)value inPlayerCheckInsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromPlayerCheckInsAtIndex:(NSUInteger)idx;
- (void)insertPlayerCheckIns:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removePlayerCheckInsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInPlayerCheckInsAtIndex:(NSUInteger)idx withObject:(CheckIn *)value;
- (void)replacePlayerCheckInsAtIndexes:(NSIndexSet *)indexes withPlayerCheckIns:(NSArray *)values;
- (void)addPlayerCheckInsObject:(CheckIn *)value;
- (void)removePlayerCheckInsObject:(CheckIn *)value;
- (void)addPlayerCheckIns:(NSOrderedSet *)values;
- (void)removePlayerCheckIns:(NSOrderedSet *)values;
- (void)addPlayerMatchesObject:(Match *)value;
- (void)removePlayerMatchesObject:(Match *)value;
- (void)addPlayerMatches:(NSSet *)values;
- (void)removePlayerMatches:(NSSet *)values;
@end
签入.h:
@class Player;
@interface CheckIn : NSManagedObject
@property (nonatomic, retain) NSNumber * challengeBadge;
@property (nonatomic, retain) NSDate * checkInTime;
@property (nonatomic, retain) NSString * player; //convenience string with player's full name
@property (nonatomic, retain) NSNumber * spiritBadge;
@property (nonatomic, retain) NSNumber * stampsBadge;
@property (nonatomic, retain) NSNumber * triviaBadge;
@property (nonatomic, retain) Player *checkedInPlayer;
@end
匹配.h
@class Player;
@interface Match : NSManagedObject
@property (nonatomic, retain) NSString * playerOne; //convenience string with player's full name
@property (nonatomic, retain) NSString * playerTwo; //convenience string with player's full name
@property (nonatomic, retain) NSDate * time;
@property (nonatomic, retain) NSString * winner; //convenience string with player's full name
@property (nonatomic, retain) NSOrderedSet *matchPlayers;
@end
@interface Match (CoreDataGeneratedAccessors)
- (void)insertObject:(Player *)value inMatchPlayersAtIndex:(NSUInteger)idx;
- (void)removeObjectFromMatchPlayersAtIndex:(NSUInteger)idx;
- (void)insertMatchPlayers:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeMatchPlayersAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInMatchPlayersAtIndex:(NSUInteger)idx withObject:(Player *)value;
- (void)replaceMatchPlayersAtIndexes:(NSIndexSet *)indexes withMatchPlayers:(NSArray *)values;
- (void)addMatchPlayersObject:(Player *)value;
- (void)removeMatchPlayersObject:(Player *)value;
- (void)addMatchPlayers:(NSOrderedSet *)values;
- (void)removeMatchPlayers:(NSOrderedSet *)values;
@end
我的模型的 Player 实体具有以下关系: playerCheckIns 到 CheckIn 与反向 checkInPlayers playerMatches 与反向 matchPlayers 匹配(这是有序的,最大为 2)
锦标赛管理员用户将使用表格视图查看所有玩家。对于每个玩家,他们将需要查看该玩家参加的比赛的列表,以及该玩家的签到列表。我想为每个可以分别应用于我的 CheckIn tableview 和 Match tableview 的谓词使用一个谓词。这不相关,但我正在使用 SWTableViewCell 将实用程序按钮放在我的自定义 tableviewcells 中,并设置手动 segues 来处理导航。 match 和 checkin 视图控制器都定义了一个 NSPredicate *predicate 来处理入站谓词。
当管理员用户在玩家单元格中选择匹配实用程序按钮时,我想加载我的匹配表视图,传递一个谓词,该谓词将过滤视图以仅显示涉及所选玩家的匹配项。这将存在于我的 prepareforsegue 方法中以获得适当的 segue 标识符。
这是我现在在玩家 tableview 的 prepareforsegue 方法中的匹配内容:
Player *currentPlayer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SUBQUERY(match, $m, ANY $m.matchPlayers == %@).@count > 0", currentPlayer];
NSLog(@"Setting predicate: %@",pred);
filteredMatchVC.predicate = pred;
我的 NSLog 显示谓词设置为:
SUBQUERY(match, $m, ANY $m.matchPlayers == nil).@count > 0
所以,currentPlayer 对象没有在谓词中被捕获。
到过滤签到视图的转换是类似的,但由于这个错误,我还没有尝试过...
非常感谢任何帮助!
【问题讨论】:
【参考方案1】:使用按钮的单元格对象作为 segue 的发送者来传递...
【讨论】:
以上是关于核心数据关系中对象的谓词的主要内容,如果未能解决你的问题,请参考以下文章