Freeze when saving an image to a database

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gollum

Hi there. So my program needed a database and I used SQLite from the godot assets library. Source: Godot SQLite - Godot Asset Library. I used this guide to save the image to the database: https://youtu.be/8gtAZuE8mBU. I saved the image to a successful database. The program worked only for very small size photos. Problem: When I try to save an image larger than 100 kb, the program freezes (very long wait). Please tell me an effective way to save an image to sqlite or a solution to the above problem. This is my diploma work. Thank you for trying to reply

:bust_in_silhouette: Reply From: codelyok13

Looks like your performing IO on the main thread and freezing all the processing till it is complete.
You should try multithreading to make sure you aren’t freezing the main thread.

var thread

# The thread will start here.
func _ready():
    thread = Thread.new()

func save_data_to_database_async(data, database):
    if thread != null:
            thread.wait_to_finish()
    thread.start(self, "save_data_to_database", {"data": data, "database": database})

func save_data_to_database(data2):
    var database = data2["database"]
    var data = data2["data"]
    database.save(data)

# Thread must be disposed (or "joined"), for portability.
func _exit_tree():
    thread.wait_to_finish()

As long as you are not saving very often, you shouldn’t get many ui hangups.

In addition to the above, I’d note that tutorial doesn’t seem overly efficient in the first place. For instance, there should be no reason to convert the image content to a string prior to storing it in the DB. You can store the binary image data directly in a SQLite BLOB field, which should be much more efficient and more compact.

I expect the example where each byte is iterated over, converted to a string rep, and then concatenated to a larger string is highly inefficient - especially with larger amounts of data (larger images).

jgodfrey | 2022-03-31 21:04

I don’t see what you are seeing. It seems like you just need to send the data to the function you want to use asynchronously. There is no need to convert your byte to a string. Please show where your are seeing this?

codelyok13 | 2022-04-01 13:03

@codelyok13 - my comments aren’t related to your above threading example. Rather, they are in regard to the video tutorial linked in the original question posted by @Gollum. The image storage code shown in that video seems very inefficient. Sorry for the confusion.

jgodfrey | 2022-04-01 13:27

Ok.
Your right, it is weird that you have to convert an image to a string.

codelyok13 | 2022-04-01 14:00