Reputation: 519
I am trying to write an application in OS X using a Realm database. I want to trigger notification when where there is change in value of List which is inside another object
Below is the Class
final class Profile: Object {
@objc dynamic var gradient1 = ""
@objc dynamic var gradient2 = ""
@objc dynamic var fontColor = ""
@objc dynamic var selectedFont = ""
@objc dynamic var selectedTitleFont = ""
@objc dynamic var fontFamily = ""
@objc dynamic var name = ""
@objc dynamic var shortbio = ""
@objc dynamic var avatarSource = ""
@objc dynamic var userid = ""
@objc dynamic var email = ""
var features = List<Features>()
var socialLinkButtons = List<SocialLinkButtons>()
@objc dynamic var appSelectedMetaData : AppMetaData? = nil
override static func primaryKey() -> String?{
return "userid"
}
}
final class Features: Object {
@objc dynamic var uuid = ""
@objc dynamic var id = ""
@objc dynamic var label = ""
@objc dynamic var screen = ""
@objc dynamic var active = false
override static func primaryKey() -> String?{
return "id"
}
convenience init(id: String, uuid: String, label: String, screen: String, active: Bool) {
self.init()
self.id = id
self.uuid = uuid
self.label = label
self.screen = screen
self.active = active
}
}
I want to trigger notifications whenever value inside feature is updated.
Upvotes: 1
Views: 758
Reputation: 54706
You can use Realm Collection Notifications to achieve your goals. You just need to make sure that you store the returned NotificationToken
in a variable that doesn't get deallocated until you don't actually need to receive the notifications anymore and that you call .invalidate()
on the token when you no longer want to receive notifications.
func observeFeatureChanges(in profile:Profile) -> NotificationToken {
let notificationToken = profile.features.observe { changes in
switch changes {
case .update(_, deletions: let deletionIndices, insertions: let insertionIndices, modifications: let modIndices):
print("Objects deleted from indices: \(deletionIndices)")
print("Objects inserted to indices: \(insertionIndices)")
print("Objects modified at indices: \(modIndices)")
case .error(let error):
print(error)
case .initial(let type):
print(type)
}
}
return notificationToken
}
Upvotes: 1