2015-12-03 35 views
1

我在Xcode 7.1.1中使用DBAccess框架v1.6.12。如何使用'DBAccess'執行事件觸發操作iOS ORM

我想用一個事件觸發時,INSERT,UPDATE或DELETE的行狀:

  1. 現有的特定時期數據的「最長」參數轉成「NO」。
  2. 查找具有最長「文本」的行。
  3. 將其行的'最長'參數更改爲'YES'。

的代碼圖像:

@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值不換款的相關信息。

回答

0

要攔截的事件,只要你想,你可以使用以下方法:

- (BOOL)entityWillInsert; 
- (BOOL)entityWillUpdate; 
- (BOOL)entityWillDelete; 
- (void)entityDidInsert; 
- (void)entityDidUpdate; 
- (void)entityDidDelete; 

從你的榜樣,雖然我可能不符合你的要求十分清楚,我會使用entityWillInsert /更新方法查詢可能更長的其他對象,然後您可以相應地更新最長的標誌。

在半僞代碼,它會是這個樣子:

- (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; 

} 
+0

謝謝你的評論。我會試試看。 – RAPT

+0

我試過了,實現了我的目標。 現在我可以理解,能夠指向嘗試完全使用'self'關鍵字在這些方法中執行記錄,如** entityWillInsert **。 – RAPT

相關問題