What is the best practice or design pattern to mai

2019-08-25 03:58发布

问题:

Scenario : Shared/Sync'd Realm. iOS/Swift App with multiple views.

What is the best practice design pattern I should use to in order to maintain synchronisation in the background - and - provide automatic (real time) updates to multiple views?

Looking at the extra task app - this makes sense but the app really only has one active view. What if I have multiple views?

Should I destroy the Realm notification of data change and rebuild for each view?

回答1:

The recommended way is the same as for a single view controller. Get a Results object for each view controller and use it directly as a data source. You can observe that Results object using Realm's notification mechanism. A notification fires when changes affecting Results any changes are committed from a background thread. The Results object has been updated to the latest state automatically, what you should do is just updating the UI.



回答2:

You may use Observer pattern. Also if you use CoreData for your models, then it's very easy too to get the update/insert/delete callback and update your view accordingly.

Observer Pattern in iOS: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

Get more details about CoreData here : https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/index.html?utm_source=iosstash.io