Search code examples
swiftrealmamazon-dynamodb

Storing large photos in Realm


I want to store a couple hundred 2 MB photos — are there any limitations or reasons why I shouldn't store this data with Realm?

The alternative would be to just save them as files and store the file paths with Realm. I'm actually leaning toward this approach because the way I'm using Realm, I occasionally clear the Realm file (e.g. when the user signs out or sometimes to avoid a migration) and then rebuild it by fetching their data over the network (I'm using Amazon DynamoDB to store their data server-side). Because of this, I don't want to have to re-fetch all the photos whenever I clear the Realm file. This is why I'm leaning toward this approach, although I have a feeling I'm not taking full advantage of Realm by doing this.

I hope this question isn't too vague, I'm basically looking for guidance on how to store large photos given how I'm using Realm. Note that the reason I'm not using Realm Object Server is simply because I've already been using DynamoDB.


Solution

  • You can store up to 16MB of data for Data or String property in Realm, see more in docs.

    However it's not recommended for images. As suggested in comments, it's better to use one of the image caching libraries instead, it will allow you not to refetch all the images and define a custom cache policy as well.