4

how can I cache array of objects in iOS using swift 3 ??

I know I can use NSCache for it the only problem is this that I know how to cache a single object but dont know how to cache a whole array of it..

var cachedData = NSCache<NSString, myObject>() // i cant define it like  NSCache<NSString, [myObject]>()  cause NSCache takes anyobject not Array of AnyObject

 //cache.setObject(myObject, forKey: "CachedObject")

anyone can guide how can I do it??

0ndre_
  • 3,577
  • 6
  • 26
  • 44
  • 1
    If you have an array, what is your intent of the cache? Simply to hold it in memory? If so, then no cache is necessary, because the array holds it in memory. Or do you want this array to persist between sessions? If so, then you'd save it to a file. Or if you're looking for `NSCache` eviction logic, then you'd retire the array altogether and just use cache. Please clarify what you mean by "cache". But the notion of a simple cache for an array just doesn't make sense. – Rob Sep 26 '16 at 16:21
  • @Rob i wanna persist it but not for a long run else i can use core-data or realm. just for a small amount of time –  Sep 26 '16 at 16:24
  • 1
    If you want it to persist solely while the app is running, just move your array to somewhere where it can't fall out of scope (e.g. the app delegate, some parent view controller, a singleton model object, etc.). – Rob Sep 26 '16 at 16:28
  • well i want something more than _while the app is running_ , i better go with storing them. thanks for helping out , appreciate your effort :) –  Sep 26 '16 at 16:36
  • Yeah, `NSCache` doesn't, itself, persist, anyway, so if you need it to survive after the app terminates, then you will have to pursue some storage mechanism. Good luck. – Rob Sep 26 '16 at 17:13

0 Answers0