0

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.

Rocky Smith
  • 195
  • 1
  • 11
  • 2
    What is the sense of doing that? Why you got that thought to download 200mb to user device? – Divers Feb 19 '17 at 21:49
  • Sorry, I wouldn't download 200mb all at once — just as individual photos are needed. What I meant to say is that the reason I'm hesitant to store the photos in Realm is because if I need to clear the Realm file I would lose all of them and have to eventually refetch them. I guess another option would be to simply create a separate Realm file for the photos. I'm just not sure if that's the right way to use Realm or not. – Rocky Smith Feb 19 '17 at 21:53
  • Just open for you Glide or Picasso library. Storing files inside realm is senseless. – Divers Feb 19 '17 at 22:07
  • According to the answer [here](http://stackoverflow.com/questions/33299523/how-to-put-an-image-in-a-realm-database/33299788#33299788): "The rule is simple: If the images are small in size, the number of images is small and they are rarely changed, you can stick with storing them in the database. If there is a bunch images, you are better off writing them directly to the file system and just storing the path of the image in the database." Seems reasonable, but I'd still like to know why (e.g. does Realm limit how much data can be added or will performance suffer). @ProblemSlover – Rocky Smith Feb 19 '17 at 22:07
  • You should store URLs to photos and display them with Glide or any other similar image loading library. You should not store large blobs of data inside the Realm file. – EpicPandaForce Feb 19 '17 at 22:47

1 Answers1

0

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.

Dmitry
  • 7,300
  • 6
  • 32
  • 55
  • Thanks! That's very helpful! Based on this [answer](http://stackoverflow.com/a/15200855/7513206), the iPhone 4s is likely to run out of memory around 286 MB, which means it's probably not a good idea to put my high res photos in Realm. I'm already using SDWebImage for image caching, but in this case I want to make sure these photos are always around, so i think I'm just going to store them in the Caches directory and store the path to the file in Realm. Thanks everyone for the help! – Rocky Smith Feb 21 '17 at 16:51