Opencc Memory Leak
近期專案上有個需求,需要使用opencc來將接收到的資料將簡體轉繁體,參考了will保哥與黑暗執行緒的範例,其中保哥的文章指出有做防止記憶體洩漏的調整,但上線後發現一樣有記憶體洩漏的問題,最後找到的解決方案是,應該要使用opencc的函式做記憶體釋放,而非使用C#的函式。 調整前 using System.Runtime.InteropServices; using System.Text; public static class OpenCCHelper { [DllImport(@"C:\Tools\opencc\bin\opencc.dll", EntryPoint = "opencc_open")] private static extern IntPtr opencc_open(string configFileName); [DllImport(@"C:\Tools\opencc\bin\opencc.dll", EntryPoint = "opencc_convert_utf8")] private static extern IntPtr opencc_convert_utf8(IntPtr opencc, IntPtr input, long length); public static string ConvertFromSimplifiedToTraditional(this string text, string config = "s2t") { return OpenCC(text, config: config); } public static string ConvertFromSimplifiedToTraditionalTaiwan(this string text, string config = "s2twp") { return OpenCC(text, config: config); } public static string ConvertFromTraditionalTaiwanToSimplified(this string text, string config = "tw2sp") { return OpenCC(text, config: config); } public static string ConvertFromTraditionalToSimplified(this string text, string config = "t2s") { return OpenCC(text, config: config); } public static string OpenCC(this string text, string config) { var configFile = $"C:\\Tools\\OpenCC\\share\\opencc\\{config}....
Dockerslow
dotnet 6 部署到docker忽然變很慢 最近在公司將dotnet 6 用docker-compose部署到測試機時忽然要等3-5分鐘,最後查證是因為我們在linux環境上有做mount遠端磁碟造成部署時間過長 掛載的磁碟機沒有做資料夾分類所有檔案都在一個資料夾底下,在根目錄就有破百萬的檔案,導致container透過volume連到實體路徑時會讀取過久
安裝到手機
如何直接安裝到ios裝置 flutter build ios flutter install 選擇要安裝到哪個裝置
2024年度目標
2024年度目標 學會使用copilot,增加coding效率 3月前使用flutter開發記帳軟體並且上架 7月前TOEIC金色證書 開始錄製廣播
Dapper PostgreSQL Error
問題 當使用dapper呼叫postgresql時後,需要在in裡面查詢一批陣列會遇到下方的錯誤訊息 var Ids = [1,2,3,4]; DynamicParameters parameters = new(); parameters.Add("Ids", Ids); var strSQL = ""; strSQL +="select * from Users where id in @Ids;" await dapper.QueryAsync(strSQL,parameters); 42601: syntax error at or near “$1”\r\n\r\nPOSITION: 81 解法 改語法為any就可以解決這個問題。 var Ids = [1,2,3,4]; DynamicParameters parameters = new(); parameters.Add("Ids", Ids); var strSQL = ""; strSQL +="select * from Users where id = any(@Ids);" await dapper.QueryAsync(strSQL,parameters);
Three Tree
三棵樹 flutter共有三個核心 widget tree 在flutter很常看到一層包一層,MaterialApp、Scaffold這類組件都是widget tree,我們通常只會對到這層。 element tree 會將資料儲存在記憶體,並決定要不要重新繪製UI render tree 是由element tree控制,當element tree決定有必要修改,就會透過render tree進行UI修改
如何固定設備方向
如何鎖定裝置的方向 先到main.dart檔案找到最初的進入點main()要執行runApp時,先執行 WidgetsFlutterBinding.ensureInitialized(); 確定每次執行都要初始化 SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,]) 選擇我們接受的方向 所在的函示庫記得引入 import ‘package:flutter/services.dart’; void main() { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, ]).then((value) => { runApp(MaterialApp( home: const Expenses(), )) }); }
資料庫分表
資料庫架構設計 架構演變 讀寫分離 當遇到資料庫瓶頸時,最簡單的做法是將資料庫做主從架構 主資料庫只有做寫入並同步資料到其他從資料庫,被同步的資料庫可以是n個只做讀取 垂直分庫 資料還是持續增長後,就到了要做垂直分庫的架構了,垂直分庫的意思可以想像成將同一個資料表內的資料欄位做整理,分成多個資料庫,也就可以將不同資料庫放在不同機器上。 資料庫分成使用者資料庫、訂單資料庫、商品資料庫 水平分庫 資料量再更大量,最後的解決方案就是水平分庫,會將相同的資料表分成多個表 table01、table02、table03 Sharding中介軟體 要做到水平分庫會建議使用已經發展成熟的Sharding中介軟體 Sharding架構 有區分成兩種 Porxy架構 應用集成架構
Pub
快速介紹flutter安裝的函式庫 intl 轉成指定地區的日期格式 flutter pub add intl uuid 建立guid flutter pub add uuid transparent_image 當圖還沒有載入時可以有透明背景圖使用 flutter pub add transparent_image riverpod 是一個狀態管理的工具,當開始寫flutter就會遇到跟現在流行的框架vue/react一樣的問題,當我要傳遞資料時要一直往子層傳參數,才能將資料傳進去,這時riverpod就是來解決這個問題的 另一個社群討論度高的是Getx。 flutter pub add riverpod 要在需要的區塊上加上ProviderScope,下列範例就是所有區塊都要可以使用 void main() { runApp(const ProviderScope( child: MyApp(), )); } 新增一個檔案並建立供應方 import 'package:flutter_riverpod/flutter_riverpod.dart'; final demoProvider = Provider((ref) { return false; }); = & ConsumerWidget StatelessWidget改成ConsumerWidget StatefulWidget改成ConsumerStatefulWidget 建立接收方 //在要使用資料的地方使用下列 //當資料不會改變時可以使用read來取代watch,但官方建議一律使用watch,以免當資料異動時,沒有修改到 final demo = ref.watch(demoProvider); //接下來即可使用demo來做後續事情 http 進行restful api,透過這個套件可以發送請求到api並接收回傳 flutter pub add http image_picker 使用系統得相機功能,在ios中記得查看文件檔,將key放入文件所說明的plist檔案中 flutter pub add image_picker...
Widget
佈局的屬性 Column 由上至下的排序 Row 由左至右的排序 Expanded 只能使用在繼承Flex以下的Widget,常使用在Cloumn或row的子項 因為有些widget不會限制長寬,要有expanded來將最大值限制在手機區塊內 ListView 使用ListView是可以有滾動效果 可以透過builder提升效能 使用方法為 ListView.builder(item: list.length, itemBuilder: (ctx,index){list[index].title}) Spacer 可以在任何row,colume使用 佔用剩餘所有空間的意思 TextField 輸入框 要記得關閉 DropdownButton 下拉式選單 Dismissable key參數必要,才能確定要滑動刪除哪筆資料 也可以分為向左滑動與向右滑動,執行不同功能 滑動刪除效果 Snackbar 會在螢幕下面調出一個訊息框 在Scaffold階層底下可以使用 ScaffoldMessenger.of(context).showSnackBar( const SnackBar( duration: Duration(seconds: 3), content: Text('Expense deleted'), ), ); InkWell 可以讓沒有事件的元件增加點擊事件之類的功能 與GestureDetector的差別在於 GestureDetector提供更多的點擊事件,InkWell提供點擊特效 兩者差異出處 Stack 類似Column與Row,但是可以讓所有子元件重疊在一起 SwitchListTile 有大標題與子標題的開關 Drawer 側邊欄,可以在裡面放Column就可以實現側邊欄的菜單 WillPopScope 當用戶離開用任何方式離開當下頁面就會觸發,不管是實體按鈕或是虛擬按鈕 onWillPop:true 允許返回false不允許返回 Form 有額外的驗證功能,並顯示在畫面 在form裡面要輸入框時要用TextFormField取代TextField 只要在Form裏面,要使用的組件基本上都會有form關鍵字 Dismissible 新增滑動移除的功能,需要增加key值才能知道要動作的是哪一個項目 後續慢慢補上