Swift Realm - writing a generi Primary Key Autoincrement function -


so in:

  1. how set auto increment key in realm?
  2. realm , auto increment behavior (android)
  3. https://github.com/realm/realm-java/issues/469

it says, primary key auto increment not supported on realm. ok, want add myself. linked posts show ways that.

problem: proposed solutions require manually add getnextprimaryid() function every model class myself. seems extremely stupid do. tried write generic 1 myself in swift have once , apply model classes automatically, failed - since not in swift.

attempt 1:

extension object {     func autoincid() -> int {         let objects = database.realm.objects(self).sorted("id")         // filter max id here , return + 1     } } 

attempt 2:

class database {         static func autoincid(type: object) -> int {         let currentid = database.realm.objects(type.self).map{$0.id}.maxelement ?? 0         return currentid + 1     } } 

both don't compile because "cannot convert value of type "object" expected argument type "object.type"".

anyone has idea how write generic function applies models automatically? call mymodelclass.autoincid() next id...

assuming you're using swift 3, should use following:

type(of: self) 

however, why want autoincrementing field? if want unique key, use uuid. autoincrementing fields cause huge problems when want app sync across multiple devices (eg. iphone , ipad).


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -