multithreading - Are there greenDAO thread safety best practices? -
i'm having go greendao , far it's going pretty well. 1 thing doesn't seem covered docs or website (or anywhere :( ) how handles thread safety.
i know basics mentioned elsewhere, "use single dao session" (general practice android + sqlite), , understand java memory model quite well. library internals appear threadsafe, or @ to the lowest degree built intention. nil i've seen covers this:
greendao caches entities default. first-class single-threaded programme - transparent , massive performance boost uses. if e.g. loadall() , modify 1 of elements, i'm modifying same object globally across app. if i'm using on main thread (e.g. display), , updating db on background thread (as right , proper), there obvious threading problems unless care taken.
does greendao "under hood" protect against mutual application-level threading problems? example, modifying cached entity in ui thread while saving in background thread (better hope don't interleave! when modifying list!)? there "best practices" protect against them, beyond general thread safety concerns (i.e. greendao expects , works with)? or whole cache fatally flawed multithreaded-application safety standpoint?
i've no experience greendao documentation here: http://greendao-orm.com/documentation/queries/
says:
if utilize queries in multiple threads, must phone call forcurrentthread() on query query instance current thread. starting greendao 1.3, object instances of query bound owning thread build query. lets safely set parameters on query object while other threads cannot interfere. if other threads seek set parameters on query or execute query bound thread, exception thrown. this, don’t need synchronized statement. in fact should avoid locking because may lead deadlocks if concurrent transactions utilize same query object.
to avoid potential deadlocks completely, greendao 1.3 introduced method forcurrentthread(). homecoming thread-local instance of query, safe utilize in current thread. every time, forcurrentthread() called, parameters set initial parameters @ time query built using builder.
while far can see documentation doesn't explicitly multi threading other this seems pretty clear handled. talking multiple threads using same query object, multiple threads can access same database. it's normal databases , dao handle concurrent access , there lot of proven techniques working caches in situation.
multithreading thread-safety greendao
No comments:
Post a Comment