swift - Should I create a separate Realm object for custom grouping? -


i have been using realm in app , love it. thank you! have question run folks , advice.

lets have realm object contains date field (simplified example):

class appointment: object {      dynamic var type = ""     dynamic var date = date() } 

now, suppose have saved thousands of appointments, , going display these on tableview or collectionview, grouped week example. datasource view this.

struct appointmentsinweek {     var startdate: date?     var enddate: date?     var appointments: [appointment] } 

so, have 2 options in mind thinking through various pros , cons: 
 a) make appointmentsinweek subclass of object, save in realm, , use datasource.

pros:
  • data table lazy loaded realm
  • simple use @ moment needed.
cons:
  • keeping date seems challenge. have kind of observable looking @ appointment in realm , added put them in appropriate appointmentweek

b) upon loading screen tableview, fetch appointments, or subset of them, group them appropriate start , end date, , create appointmentsinweek struct use datasource.

pros:
  • appointmentsinweek date because created on fly needed
cons:
  • we have keep of in memory, limiting amount of appointments realistically display @ once.

i started option b thinking might better go option a. if do, biggest issue making sure realm date when new appointments added.

questions

  1. are there other options did not consider?
  2. which sounds better option?
  3. assuming go option a, make sense have class, lives throughout life of app, in charge of observing appointments in realm , when 1 added (or changed), add appropriate appointmentweek?

both options fine, suggest option a. there few ways approach option a.

first of all, don't need keep appointment , appointmentsinweek in sync manually. can use combination of object properties, list properties, , linking objects properties model connections between appointments , appointmentsinweeks. how might implement depends on app's specific needs, here's 1 possibility:

class appointment : object {     dynamic var type = ""     dynamic var date = nsdate()     // link week appointment lives in, if desired     var week: appointmentsinweek? = nil }  class appointmentsinweek : object {     dynamic var startdate = nsdate()     dynamic var enddate = nsdate()     // add appointment list whenever it's created     let appointments = list<appointment>() } 

a second possibility not use relationships @ all, use queries instead. realm supports queries through results class. add ignored property on appointmentsinweek class queries realm appointments fall within date range:

class appointment : object {     dynamic var type = ""     dynamic var date = nsdate() }  class appointmentsinweek : object {     dynamic var startdate = nsdate()     dynamic var enddate = nsdate()      lazy var appointments: results<appointment> = {         // implementation assumes can reference         // realm storing appointments somehow         return appointmentsrealm.objects(appointments.self).filter(nspredicate(format: "(date >= %@) , (date < %@)", self.startdate, self.enddate))     }()      override static func ignoredproperties() -> [string] {         return ["appointments"]     } } 

in either case realms, lists, , results update automatically whenever runloop of thread on (usually main thread) runs. realm supports variety of notifications in case need react changes manually.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -