What is the best practice or design pattern to mai

2019-08-25 03:41发布

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?

2条回答
不美不萌又怎样
2楼-- · 2019-08-25 04:24

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

查看更多
家丑人穷心不美
3楼-- · 2019-08-25 04:26

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.

查看更多
登录 后发表回答