c# - Realm async usage in Tasks -


i'm trying better understand how use realm in async scenario. far i'm aware realm instance can access objects on thread created. async transaction need new instance of realm using getinstance("somerealmfile");.

there function called task writeasync(action<realm> action) can execute write transaction on worker (background) thread.

here's example scenario. app needs make web request. runs async task function go off web , fetch data. request returns , app wants take data parse in realm object , persist database using realm.manage<t>(t obj). note entire scenario assumes have created realm database on ui main thread begin with.

let's assume want stay on background thread @ point, in context of async task function started web request.

so we're in situation have results of web request (data) via var results = await webfetch();, possibly using taskcompletionsource instead , can realm write transactions on worker thread. assume call async write transaction @ point need new instance of realm , pass writeasync() function.

this raises question, let's assume before database write transactions want queries , check few things before write stuff. happens if these queries in async function? because i've run in lot of weirdness trying before, works , queries return null (depending on type of query of course). queries work faultlessly on main thread.

edit bug, queries in async function have been unreliable.

so clarity, we're still on assumption have returned async task (web fetch) , we're doing few realm queries , calling task writeasync(action<realm> action) function write data database. correct approach? clarity on 1 great.

one final point, async write transactions necessary? sort of realm scenario (write transaction) might block ui thread long enough require , justify executed on worker thread (from mobile development perspective only).

edit answered async example linked below, process takes lot of cpu resources. 1 assume basic realm write transactions, example managing list of 50 objects shouldn't need async?

a pseudo code sample great.

edit added code sample off top of head.

async task realmwritesasync {     var result = await webfetch();      // maybe queries here     var realm = realm.getinstance("realmfile");     await realm.writeasync(realm =>     {           // realm.manage results     }); } 

environment = xamarin.ios/droid project pcl using mvvmcross


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -