android - Most efficient way to share data between a Service and an Activity -
i'm working on app composed of background intentservice running time (even when activity lost focus) , activity (a section pager fragments).
the background service checking time , storing large array of objects (up 300) parsed xml file.
each object represent event associated starttime , when starttime matches current time service has notify activity (if available) display event happening.
on activity side have fragments (tabs of section pager) custom arrayadapter list events listview.
right i'm doing sending object service localbroadcastmanager. fragments listening , when receive object update local arraylist replacing , notifying arrayadapter data set changed.
since each fragment has keep arraylist update arrayadapter , listview, end same object stored both inside service's array , in 1 of fragment' array. i'm wondering if there better way.
the question : efficient (on memory/cpu usage , speed) way share large array of objects between service , activity ?
you use greenrobot's eventbus. don't think can avoid duplicating data. if use same object instance in service , in fragments, you'll have take care notify adapters each , every time, otherwise you'll errors.
you better off storing downloaded objects in local sqlite database , loading minimum amount of information needed in fragments. minimize time takes transfer data intentservice thread activity's main thread (because wouldn't transfer data, send signal current fragment data has been updated). contentobservable may you.
also, shouldn't use intentservice tasks run time. intentservice intended run single task , finish, being started again next time need run task. should use plain service thread or handlerthread inside it. have better control way, , able run multiple tasks in parallel if need to.
Comments
Post a Comment