ios - Realm.write is being skipped -


i using realm , trying reorder cells in tableview. behaviour find odd works want (it updates data in realm properly) when hits line:

myrealm.write { 

it not go inside block. using breakpoints can see skips whole block , goes directly end of block. because of this, even-though gives visual appearance tableview has been re-ordered, has not been re-ordered in realm.

i should note using column in table in realm called order means of storing order. also, labels array realm of table trying order

override func tableview(tableview: uitableview, moverowatindexpath sourceindexpath: nsindexpath, toindexpath destinationindexpath: nsindexpath) {          //update when move row         if sourceindexpath.row > destinationindexpath.row {             let lowerbound = destinationindexpath.row             let upperbound = sourceindexpath.row             do{                 let myrealm = try realm()                  myrealm.write {                      let labelatsource = self.labels![upperbound]                     labelatsource.order = lowerbound                      in lowerbound...upperbound-1{                         let label = self.labels![i]                         label.order = i+1                     }                 }                }catch { }         }else{ //when move row down             let lowerbound = sourceindexpath.row             let upperbound = destinationindexpath.row             do{                 let myrealm = try realm()                  myrealm.write {                      let labelatsource = self.labels![lowerbound]                     labelatsource.order = upperbound                      in lowerbound+1...upperbound{                         let label = self.labels![i]                         label.order = i-1                     }                 }             }catch { }         } } 

you're calling throwing initializer realm() in do-catch block without error handling @ all. i'd guess fails. instead of catching error , let disappear unhandled, i'd rather recommend use try!, cause runtime error here.

but should ensure have accessed realm before @ least once avoid having worry failing schema migrations / validations @ point. errors can still occur later default realm setup typically seen during development, e.g. when browser attached simulator files.


Comments