I am trying to bring some order and clarity into my understanding of Rho BLOBs. For someone who's never been dealing with something like Rho, I find it incredibly hard (and I have to tech newcomers).
There is a lot of docs/references and RhoConnect BLOB sync parths, but not a single tutorial on BLOBs in general.
So, below is "Rho BLOB beginner guide, as I understand them" draft .
Can you please provide feedback (is my understanding correct and full?), answer the questions in red and share your thoughts and experiences with BLOBs?
Let's build a community guide!
Normally, RHOM stores data in the local SQLite DB. However, large binary objects (pictures, files, etc) are not stored there.
[ Any particular reason for that? Performance? Memory? OS restrictions? Is this the reason why: Re: What is the maximum size of the Web SQL database? ]
Instead, RHOM stores them is a special folder on the device (Rho::Application.databaseBlobFolder, default: “<app_path>/db/db-files/”) and stores a relative PATH (URI, to be precise) to them in the SQLite database.
Working with BLOBs
Thus, every time you want to address the BLOB:
- Get the BLOB URI from the Model records: @product.myblob
- Convert that URI to an absolute file path: localpath = Rho::RhoApplication::get_blob_path(@product.myblob)
- [I found it to work with relative URIs pretty well, why do I need to convert to absolute path every time?]
- [Why wouldn't I store the absolute path in the DB instead of URI then? RhoConnect requirements?]
- Do something with the file: <img src="localpath">, File.delete(locapath), etc..
Sounds simple, right? We'll it's not that hard, really. However, there are some important things to remember:
RhoConnect and sync in general
RhoConnect sync client (client! In the app!) will recognize BLOBS as BLOBS if model has property set up as BLOB: property :myblob, :blob
Then, RhoConnect will sync the file DATA (and update URIs in SQL database automatically). You still need to implement a source adapter method to deal with those BLOBS.
More here: Rhomobile | Binary Objects and Blob Sync
If you're not using RhoConnect for sync - you're on your own. Also see the next section.
BLOB File Management
Other than RhoConnect, no file management happens with BLOBs [true?]. For example, when you have a record with a BLOB in it (and associated file), the file will not be deleted after you delete the record.
Same, if you, say, change the picture, associated with your records - old file will remain. With time, this junk accumulates to great proportions.
Thus, you have to implement file management yourself, in controller or model (where applicable). Here's a quick example of hacking update_parameters in model
alias_method :update_attributes_orig, :update_attributes def update_attributes(update_hash) if update_hash['Picture'] # This is equivalent to: old_url = @vars[:Picture] ? @vars[:Picture] : '' old_url = @vars[:Picture] || '' File.delete(old_url) if File.exists?(old_url) && update_hash['Picture'] != old_url end update_attributes_orig(update_hash) #call the original method end
- BLOB is a file stored in a special folder
- Only reference stored in the URI, which has to be converted to absolute path
- RhoConnect handles sync automatically at the client side
- But for everything else you have to implement your own file management and clean up.
- As a workaround - can change the DB size and store BLOBS as Base64 encoded strings. Then decode and use them where needed.
Thanks for your time. Please let me know your thoughts!