Handling Messages from PubNub History as a “Global Variable”

眉间皱痕 提交于 2019-12-04 19:47:47

For me, singleton always was a tool which is served for concrete purpose. I don't see any troubles event with multiuser and their switch based on singleton (you can always reset state of singleton and not try to destroy it).
Because we work a lot with data, we can't afford to initialize some data model all the time when we need to use it, because there will be spend additional time on: allocation & initialization, presetting initial state, maybe loading some data from file system or network. If we will perform all this steps in each view or data object where we would like to manipulate with data - it will kill our app responsiveness and user will have some bad experience.
So, it all depends on how you will code your singleton and how you will use it. If you have multiuser app and some sensitive data should be keep away from another user you can create some User entities and store all required data in it (in your case messages can be passed to it). This entity will be some simple data model or data object and still you will be required to have some class responsible for it. Here you can create some singleton which will be responsible for managing current user and his data (when you specify current user, old one can be deallocated and all data can be pushed into some local storage like CoreData on sqlite).
As I understand, you chat application want to pull out user history after login... You can do it in class which will manage User entity and fetch history as soon as current user will be changed (in you example you call history API each time, when new message arrive to the user - bad idea because history API may be heavy). Your PFUser instance can store all required data as for channels on which it should be subscribed or unsubscribed and they can be used during logOut process.

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