jeudi 17 décembre 2015

Managing objects within the application

I'm struggling with one design problem for my application.

I have multiple views which can share same objects downloaded from server API. As a simple example: "list view" and "detail view". To display the "detail view" I'm calling an API which returns much more data then I got from "list view" API - also object could change in between, which means that API call that I do on "detail view" should update data on the list view to make sure those are in sync (on iPad those two are visible at the same time).

All objects has unique identifiers so what I have right now is centralized cache with NSDictionary containing "objectId" => "object" and every time I download an object from server, I check if there is existing instance of it - I simply update it. After updating the object, I post NSNotification through NSNotificationCenter with object attached. All my views that may require refreshing are listening for this notifications and compare objectIds and decide if they should refresh something.

But with that solution I have one big problem:

  1. I don't when I can remove objects from cache or how to clean cache in case of memory warning. Problem is that there is no way to figure out if object is still used by any of the existing views.

I've been thinking about using other patterns, but I always find some blockers with those:

  • KVO - same problem as in my current design
  • delegate patterns - there can be only one delegate at a time for an object and I need multiple listeners

What would be the best solution to share data across multiple views and still be able to figure out if any of locally kept objects can be removed from cache ?

I don't want to involve CoreData at this stage of the project yet.

Aucun commentaire:

Enregistrer un commentaire