What is the best practice or design pattern to maintain sync activity across multiple views?

坚强是说给别人听的谎言 提交于 2019-12-25 07:49:56

问题


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



来源:https://stackoverflow.com/questions/41179789/what-is-the-best-practice-or-design-pattern-to-maintain-sync-activity-across-mul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!