Open rangerzhou opened 3 years ago
thats not a problem from the chart itself. its more like how you pass the data to your list and get it. arraylist etc are not threadsafe producer adds new data while consumer read it. you have to syncronize your calls to the list to make sure the list gets not modified while a new item get added or it get read by the lib
thats not a problem from the chart itself. its more like how you pass the data to your list and get it. arraylist etc are not threadsafe producer adds new data while consumer read it. you have to syncronize your calls to the list to make sure the list gets not modified while a new item get added or it get read by the lib
Thanks, but I had try to add synchronized in method, the exception still occured, then I execute update chart in runOnUiThread(), the exception gone.
syncronize methods only lock the execution for that method. what you need is something like
private ArrayList<YourItem> items = new ArrayList();
public void add(YourItem item)
{
//lock the list. all other producers/consumers that want to get access to the list have to wait till the block is executed
syncronized(items)
{
items.add(item);
}
}
public void updateChart(Chart chart)
{
syncronized(items)
{
chart.setList(items);
chart.invalidate();
}
}
this works most time aslong you dont interact with the chart itself and force it internal to redraw. what i mean with that is like rotating the piechart. in that case it could happen that it throws a nullpointer if the index get accesed while the chart get updated. the chart internal access to the list need also to get synced. then it would be ready to handel realtime updates
why it works with runOnUiThread is because that stuff run one after one, also if get posted from a different handler wich runs on the MainLooper. Threading on android can get realy complicated.
I know what you mean, thank you for your detailed answer!
Summary
Expected Behavior
Possible Solution
Device (please complete the following information):
Additional Context
ADD A REWARD using Speed to SOLVE this issue QUICKLY and SUPPORT this project.
I do not known the reason, can anyone help me? Thanks!!!