我正在使用带有Xcode 7.1.1的DBAccess框架v1.6.12。
我想在INSERT,UPDATE或DELETE行中使用事件触发器,如:
代码图片:
@interface NoteModel : DBObject
@property uint32_t dateYMD; // not unique
@property BOOL longest; // default value is NO
@property NSString *text;
@end
- (void)test {
NoteModel *obj = [NoteModel new];
obj.dateYMD = 20151201;
obj.text = @"hoge";
[obj commit]; //< HERE I want to fire the event trigger
}
DBObject#entityWillInsert只是返回BOOL值而没有chaging信息。
答案 0 :(得分:0)
要根据需要拦截事件,您将使用以下方法:
- (BOOL)entityWillInsert;
- (BOOL)entityWillUpdate;
- (BOOL)entityWillDelete;
- (void)entityDidInsert;
- (void)entityDidUpdate;
- (void)entityDidDelete;
从您的示例中,虽然我可能不太清楚您要问的是什么,但我会使用entityWillInsert / Update方法查询可能更长的其他对象,然后您可以更新最长相应地标记。
在半伪代码中,它看起来像这样:
- (BOOL)entityWillInsert {
// see if we have any existing records with a longer text field
self.longest = [[[NoteModel query] whereWithFormat:@"length(text) > length(%@)", self.text] count] ? NO:YES;
// now if this is to be the longest then we will need to ensure that the current record is updated too.
if(self.longest) {
for (NoteModel* r in [[[NoteModel query] where:@"longest = 1"] fetch]) {
r.longest = NO;
[r commit];
}
}
// you must return yes to ensure the ORM knows to complete the action
return YES;
}