c# - Akavache's GetObject<T> hangs when awaited. Any idea what is wrong here? -


i have xamarin.forms application, code in app class (yes, sample demonstrate issue):

    public app()     {         blobcache.applicationname = "myapp";         blobcache.ensureinitialized();           // root page of application         mainpage = getmainpage();     }      public object blockinggetexternaluser()     {         return getexternaluser().result;     }      private async task<object> getexternaluser()     {         try         {             return await blobcache.localmachine.getobject<object>("user");         }         catch (keynotfoundexception)         {             return null;         }     } 

the key "user" not exist, expect keynotfoundexception. never see exception being thrown. instead "hangs" , never returns await getobject call.

i running on phone android 5.0.

any ideas how fix this? doing fundamentally wrong?

update: on side note: instead of trying getobject, 1 try check if key exists in cache , retrieve cache. however, if not mistaken, there no other way check other calling getobject , catching exception in sample above. scenario 1 want know if item exists, doesn't seem ideal. maybe "exists()" method nice have in akavache? or maybe missing something?

update2: changing example not use async method in constructor. prove point that not issue.

update3: removing call constructor. when call blockinggetexternaluser anywhere in code, await still hang.

you're having dead-lock. quoting synchronously waiting async operation, , why wait() freeze program here:

the await inside asynchronous method trying come ui thread.

since ui thread busy waiting entire task complete, have > deadlock.

note call .result implies task.wait() somewhere.

there 2 solutions: either avoid async methods, or wrap code task.run this:

public object blockinggetexternaluser() {     return task.run<object>(() => getexternaluser().result); } 

(i hope it's compiling didn't verify in vs :)

by experience tend avoid async methods in combination sqlite these days. reason sqlite wrapper libraries use task.run anti-pattern provide async wrappers around methods while sqlite doesn't have intrinsic notations of being asynchronous. note though it's fine wrap things task.run make them asynchronous , it's anti-pattern library designers, suggesting users methods asynchronous when they're not. can read more here: task.run anti-pattern?


Comments