i have image in core data. has saved nsdata, , have computed property , set image based on core data property imagedata
:
var image: uiimage? { { if let imagedata = imagedata { return uiimage(data: imagedata) } else { return nil } } set { if let image = image { imagedata = uiimagepngrepresentation(image) } else { imagedata = nil } } }
however, believe code convert between uiimage
, nsdata
every time image fetched, can since data used populate uitableviewcell
. true, or xcode smart enough cache computed properties in way? also, recommended way of fetching images core data, or recommended fetch once , save new property (unsure if code correct):
lazy var image: uiimage? = { if let imagedata = imagedata { return uiimage(data: imagedata) } else { return nil } }()
the downside of last 1 way see possible change imagedata
without image
being updated. i'm not sure if can use didset
in latter version update core data.
assuming imagedata
property of entity description,
- the managed object context cache
imagedata
. - the
image
not cached, , recomputed every time.
if you're going store images in core data, right approach simpler you're making it. uiimage
conforms nscoding
, means can create image
attribute core data "transformable" type. core data automatically invoke nscoding
methods convert to/from uiimage
, don't need of code in question. read/write uiimage
.
if you're going store images in core data, using any scheme, make sure images aren't large. core data can handle them may find image data gets loaded when don't expect. example, if you're using other non-image attributes of managed object, image data gets loaded anyway.