Closed AlienKevin closed 2 years ago
Thanks to @fzyzcjy for kindly pointing out the solutions to the two issues I encountered.
Replace thread_local RefCell
with Mutex
prevents Once poisoning. You can use either the Rust standard library's std::sync::Mutex
or Mutex provided by the parking_lot
crate. Parking_lot claims that their mutexes have no poisoning and uses less memory. I used the parking_lot's mutex in conjunction with flutter_rust_bridge and everything is fine so far. If you are not familiar with Mutex, check out the Shared State Concurrency section of the Rust Book.
To manage local Rust states with Mutex, you need to use it in conjunction with the lazy_static
library.
Api
state
lazy_static! {
static ref API: Mutex<Option<Api>> = Mutex::new(None);
}
init_api
function that can be called from Dart to initialize the Api
struct in Rust:
pub fn init_api(input_app_dir: String) -> Result<()> {
*API.lock() = Some(Api::new(input_app_dir));
Ok(())
}
pub fn pr_search(capacity: u32, query: String) -> Result<Vec<PrSearchResult>> {
Ok((*API.lock()).as_ref().unwrap().pr_search(capacity, &query))
}
println!()
not workingThis solution targets iOS logging. Check out the android_logger crate if you want to enable logging on Android.
You need external rust bindings to Apple's universal logging system for iOS logging to work. The only functioning library that I can find is oslog, which implements the logger interface set out by the log
crate. Follow the steps below to set up iOS logging:
use oslog::{OsLogger};
use log::{LevelFilter, info};
log
crate from throwing Logger already set error
when you Hot Restart the app and the init_api
function got called again in Dart.
static ref IS_LOG_INITIALIZED: Mutex<bool> = Mutex::new(false);
Api::new()
if !*IS_LOG_INITIALIZED.lock() {
OsLogger::new("com.example")
.level_filter(LevelFilter::Debug)
.category_level_filter("Settings", LevelFilter::Trace)
.init()
.unwrap();
*IS_LOG_INITIALIZED.lock() = true;
}
info!()
to print debug messages
info!("Your debug message goes here");
🎉 🎉 🎉
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.
Discussed in https://github.com/fzyzcjy/flutter_rust_bridge/discussions/251