Responsibility of maintaining App's datastore.

Discussion in 'iOS Programming' started by mycompuser, Sep 3, 2015.

  1. mycompuser macrumors member

    May 8, 2012
    I am in the process of developing an datastore for my iOS app.

    I have written an datastore which is an wrapper around NSMutableArray (say method "addObjectToDataStore" used to add data to the datastore).

    I use the datastore throughout the various classes in my app to collect data (key/value pair) which I save onto it using static method exposed by the datastore class. Archiving of data on app exit and unarchiving of datacollector data is done my main appdelegate class.

    This data from the datastore I read periodically and use for reporting purpose.

    I'll have to trim/remove outdated/unused/stale data periodically (say once a day), so that the datastore size does not bloat.

    My questing is with respect to the trimming of datastore data.
    Whose responsibility is it to trim the data? I plan to expose an method "trim" in datastore class which might accept the trimming criteria(say max file size) and accordingly trim the data.

    But not sure if that violates the "Single responsibility principle" for datastore class.
    And who should have the responsibility of calling this "trim" method?

    Should I call the "trim" method form the main appdelegate class or should I call the "trim" method from within "addObjectToDataStore" method of the datastore class such that when the criteria is satisfied, it would trim the data?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You might want to expose the trim method but best is if the datastore class does the trimming itself in response to whatever criteria to set for when to do the trimming. Your datastore class could set a timer that runs once per day so it gets called back when it needs to do this. It could register for various UIApplication life cycle events so that trimming happens when the app is going into the background. Consider that the trim operation might take some time so you may not want to do it as part of a request to add an object (but maybe you do).
  3. mycompuser thread starter macrumors member

    May 8, 2012

    Thanks for the reply. I think your approach is the best.

    Have implemented it as per the same where the method to trim is called from within the datastore class when the criteria is met.

Share This Page