2

I want to update multiple record (DBAccess ORM). by condition with specific fields. Like set city = "Goa" where name = "atul".

Please review following swift code, it working fine. But how to do this by single query without using for loop.

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

Please suggest perfect solutions to reduce number of lines/loops and improve above query code.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45

1 Answers1

0

There is no way too, except for executing raw SQL which is inherently dangerous, especially as the cached and open objects then have no idea of the value that have been updated in SQLite.

Is there a problem looping objects? If it's performance, then wrap the update inside a transaction and that will speed it up.

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

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

Also upgrade to SharkORM, as it has now taken over from DBAccess:

http://sharkorm.com, https://github.com/sharksync/sharkorm

You could always add an update feature to the query module if you wanted. Or raise a feature request via issues.

Adrian_H
  • 1,548
  • 1
  • 14
  • 27