Saving Array to Realm in Swift? -
is possible save array of objects realm
? anytime make change array should saved realm.
my current solution save object object for loop
. append/modifying objects calling save()
job, not when remove object it.
class customobject: object { dynamic var name = "" dynamic var id = 0 override static func primarykey() -> string? { return "id" } } struct realmdatabase { static var sharedinstance = realmdatabase() var realm: realm! let object0 = customobject() let object1 = customobject() var array = [object0, object1] init() { self.realm = try! realm() } func save() { object in self.array { try! self.realm.write { self.realm.add(object, update: true) } } } }
to save lists of objects have use realm list
, not swift array.
let objects = list<customobject>()
then, can add elements:
objects.append(object1)
take @ to many relationships , collections sections of the official docs.
Comments
Post a Comment