Sharing UICollectionView data from 3 view controllers using MVC iOS -
this scenario.i working on app there 3 view controllers(say "child1","child2","child3") each collectionview on it.now of them have same functionality.the difference data coming 3 different apis.now have created parent view controller(say "parentvc") , 3 viewcontrollers inherit "parentvc".
now suppose arrive on "child1",api hits , collectionview reloaded. push "child2",api hits , collectionview reloaded. similar case "child3".
now when pop "child3",the api on "child2" should not hit again. similar case when pop "child2".
now have managed that,but here problem.
problem:
i trying use mvc pattern,and have separate data model class takes data api.now how can use mvc in scenario,as data model needs updated everytime switch between view controllers , therefore reload operation needs done?
you have 2 options solve problem
1- try call api in viewwillappear instead of viewdidload.
2- if using mvc need implement 2 methods in model classes called encodewithcoder , initwithcoder , after getting response api save model objects array or dictionary in nsuserdefaults using below code
nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; nsdata *myencodedobject = [nskeyedarchiver archiveddatawithrootobject:object]; [prefs setobject:myencodedobject forkey:key]; [[nsuserdefaults standarduserdefaults] synchronize];
and saved object using below code.
nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; nsdata *myencodedobject = [prefs objectforkey:key ]; id obj = [nskeyedunarchiver unarchiveobjectwithdata: myencodedobject];
Comments
Post a Comment