cunarist / rinf

Rust for native business logic, Flutter for flexible and beautiful GUI
MIT License
1.99k stars 72 forks source link

Can I use sqlite? #133

Closed hoslo closed 1 year ago

hoslo commented 1 year ago

sqlite is not working

use tokio::sync::OnceCell;

const DB_URL: &str = "sqlite::memory:";

static POOL: OnceCell<Pool<Sqlite>> = OnceCell::const_new();

async fn get_global_db<'a>() -> &'a Pool<Sqlite> {
    POOL.get_or_init(|| async {
        SqlitePoolOptions::new()
.connect(DB_URL).await.unwrap()
    }).await
}

async fn main() {   
    let mut request_receiver = bridge::get_request_receiver();
    // It looks like the function is blocking here
    get_global_db().await;

    while let Some(request_unique) = request_receiver.recv().await {
        crate::spawn(async {
            let response_unique = handle_request(request_unique).await;
            respond_to_dart(response_unique);
        });
    }
}
temeddix commented 1 year ago

Yeah, it should possible to use sqlite as well as any Rust crates. I wonder why it's blocking at that point, isn't there any panic going on at unwrap()?

hoslo commented 1 year ago

Yeah, it should possible to use sqlite as well as any Rust crates. I wonder why it's blocking at that point, isn't there any panic going on at unwrap()?

It is possible to use sqlite in memory, but not if you use files

const DB_URL: &str = "sqlite:test.db";
temeddix commented 1 year ago

That's strange. It should just work as well as all other Rust crates. I'll take a look later when I have time :|

temeddix commented 1 year ago

Hmm, this doesn't look like an issue with RIF.

Could you try these and tell me if the problem persists?

P.S. File operations won't work if you run or build the app for the web.

hoslo commented 1 year ago

Hmm, this doesn't look like an issue with RIF.

Could you try these and tell me if the problem persists?

P.S. File operations won't work if you run or build the app for the web.

The above two methods can automatically create sqlite files in rust binary programs, but flutter can not, I'm not sure if trying rust in flutter can we manipulate files directly in rust code (native)?

temeddix commented 1 year ago

If I understood correctly, that those two methods can automatically create sqlite files in 'Application folder', is this right? Or is it that sqlite files are embeeded in .dll, .a, .so binary files?

Could you provide some more information like...:

hoslo commented 1 year ago

If I understood correctly, that those two methods can automatically create sqlite files in 'Application folder', is this right? Or is it that sqlite files are embeeded in .dll, .a, .so binary files?

Could you provide some more information like...:

  • Which OS are you developing on?
  • Which OS are you targeting?
  • Do you have error messages from the CLI?
  • Is it some kind of permission problem?

I'm developing on macos (m1 chip), targeting the iphone emulator, and I don't see task error messages from the cli, except that each request fails after a timeout

temeddix commented 1 year ago

I see 🤔 do you mind showing me your code, perhaps by sharing your repository address?

One of the reasons could be the app sandbox policy of iOS. Can you try running your app on macOS and share the result?

hoslo commented 1 year ago

I see 🤔 do you mind showing me your code, perhaps by sharing your repository address?

One of the reasons could be the app sandbox policy of iOS. Can you try running your app on macOS and share the result?

you can look at this repo https://github.com/hoslo/tally

temeddix commented 1 year ago

Sorry to say this, but I might be busy for a while, so if you happen to find a solution, please share the idea here or in a new pull request. If I become free again, I will dive into this soon. Thank you for your patience :)

temeddix commented 1 year ago
image

It does work on Windows, other platforms coming soon

temeddix commented 1 year ago
image

It also runs well on macOS

temeddix commented 1 year ago

This was an interesting issue, though it wasn't related to RIF.

Now I understand why it was not working on iOS. The Rust thread panicked because you didn't have the permission to store test.db beside the executable binary. In iOS, you are only allowed to access specific folders of the app itself. Also, you didn't add ?mode=wrc to the end of the sqlite path to create one if not present.

To understand what sandboxing is in iOS apps, take a look at this post:

I was able to fix your app to make it work on iOS. Take a look at my fork: https://github.com/hoslo/tally/compare/master...temeddix:tally:master