2016-07-20 37 views
3

我想更新多個記錄(DBAccess ORM)。由具體領域的條件。像設置城市=「果阿」,其中名稱=「atul」。如何在swift中使用dbAccess的特定字段更新多個記錄

請檢查下面的swift代碼,它工作正常。但是如何通過單個查詢來做到這一點,而不使用for循環。

func updateRecordsByName(userName: String) { 

    //like userName = atul; 
    let userArr : DBResultSet = User.query().whereWithFormat("name = %@", withParameters:[userName]).fetch(); 

    for data in userArr { 

     (data as! User).city = "Goa"; 

     (data as! User).commit(); 
    } 
} 

請提出完美的解決方案,以減少行數/循環次數並改善上述查詢代碼。

回答

0

除了執行固有危險的原始SQL以外,沒有辦法,尤其是當緩存和打開的對象不知道已在SQLite中更新的值時。

循環對象有問題嗎?如果是性能,那麼將更新包裝在一個事務中,這會加快速度。

DBTransaction.transaction({ 
     // put all your writes here 

     for data in userArr { 
      (data as! User).city = "Goa"; 
      (data as! User).commit(); 
     } 
    }) { 
     // rollback 
    } 

也升級到SharkORM,因爲它已經從DBACCESS接管:

http://sharkorm.comhttps://github.com/sharksync/sharkorm

你可以隨時添加的更新功能,查詢模塊,如果你想要的。或通過問題提出功能請求。

相關問題