Store both NSData(UIImage) & NSString as object in the same table database2 ?

22 views
Skip to first unread message

lang qiu

unread,
Mar 10, 2017, 2:06:03 AM3/10/17
to YapDatabase
I plan to use YapDatabase for my chat app, storing each message into |collection|key|object|metadata|. Each message has a unique id, which will be used as key; message content normally is nstring, which will be used as object, timestamp with some other data is used as metadata. So far so good.

But sometimes a picture will be sent. My knowledge with RMDB tells me I should store pictures in another table. When I check the YapDatabase code I find the actual sqlite table YapDatabase creates is database2, CREATE TABLE "database2" ("rowid" INTEGER PRIMARY KEY, "collection" CHAR NOT NULL, "key" CHAR NOT NULL, "data" BLOB, "metadata" BLOB ). So it seems that I can't store them in another table. Now if I want to do a simple query, e.g. find all my text messages there seems to be no easy way to do it.

So is my concern valid? Do I need to store them in separated tables? If yes, how?  If not, how do I  find all my text message easily?

Thanks

Qiulang

Joel Ekström

unread,
Mar 10, 2017, 4:54:06 AM3/10/17
to yapda...@googlegroups.com
Hi!

There are multiple approaches for this, and hopefully I can clear it up a bit for you. Generally, if you have many (and/or large images), what you would store in your model object is a pointer (file URL/path as string) to the image file. You then store the image file itself outside of the database, like a normal file (using NSFileManager for example). So there is no point in storing the images in a different table - what you probably want is to not have images in YapDatabase at all.

With that said - it's certainly possible to store images in databases. The reason to do it with a file pointer instead is that databases usually aren't optimised for large blobs of data like images.

You're saying that "sometimes a picture will be sent". If it's not common, and the images are small (like an emoji), then I see no reason to complicate things. In this case, you could store it in your model object directly. UIImage supports NSSecureCoding, so it's simple to store it just like the rest of your properties.

It's hard for a bystander to give you advice on which way to go with here. I think you need to evaluate your app, and consider the frequency/size of the images sent. If the images are large, like photos, or if they will be sent often, then I would recommend going with the file-based approach.

lang qiu

unread,
Mar 10, 2017, 5:16:15 AM3/10/17
to YapDatabase, li...@ekstrom.io
Thanks for the reply. The images are small, normally around couple hundreds of kbs. I don't want to store them as files, I believe storing them as blob is appropriate. 

The thing that bothers is me is that if I don't use YapDatabase I will use another table to store them (And actually I did that in other project). But if I use YapDatabse, they are stored in the same table as my normally text messages, how can I do some query like I said, find all my text message.

Thanks

Qiulang
Reply all
Reply to author
Forward
0 new messages