ios - Not need to update object in Realm -
i try read json , create realm, code:
func workwithfilejson () { //local file json let file = bundle.main.path(forresource: "mobiledbschema", oftype: "json")! let url = url(fileurlwithpath: file) let jsondata = nsdata(contentsof: url)! //parce json let json = try! jsonserialization.jsonobject(with: jsondata data, options: []) try! realm.write { //create data json our objects realm.create(dataroot.self, value: json, update: true) } }
and file classes:
import foundation import realmswift class dataroot: object { dynamic var id = 0 dynamic var name = "" let transport_type = list<transport_type>() override class func primarykey() -> string? { return "id" } } class transport_type: object { dynamic var id = 0 dynamic var name = "" let routes = list<routes>() override class func primarykey() -> string? { return "id" } } class routes: object { dynamic var id = 0 dynamic var name = "" let directions = list<directions>() override class func primarykey() -> string? { return "id" } } class directions: object { dynamic var id = 0 dynamic var name = "" dynamic var dayidfrom = 0 dynamic var dayidto = 0 let stops = list<stops>() override class func primarykey() -> string? { return "id" } } class stops: object { dynamic var id = 0 dynamic var busstop: busstop? let timetable = list<timetable>() override class func primarykey() -> string? { return "id" } } class busstop: object { dynamic var id = 0 dynamic var name = "" dynamic var descript = "" override class func primarykey() -> string? { return "id" } } class timetable: object { dynamic var hour = 0 dynamic var minute = 0 dynamic var group_index = 0 dynamic var notes = "" }
after first run see data in realm:
but after second run see data in timetable
x 2 , etc. time after each run.
in timetable there no primary keys (here don't need it). why after each update (run) see increase data in timetable
, how resolve mistake?
even if app doesn't need primary keys, realm.add(_:update:)
requires object
class implement 1 able identify pre-existing entries opposed new ones. if not specify primary key, if update:
set true
, add each item json new object.
ideally, should able implement kind of primary id each entry in json feed can pass along realm.
however, if cannot implement primary keys, know every new json object pull down complete snapshot of timetables, consider deleting of pre-existing timetable objects in realm file before adding latest ones json file.
Comments
Post a Comment