Fix conflicts in friendlist refresh.

This commit is contained in:
Pierre 2018-05-20 18:15:14 +02:00
parent e582053655
commit 889e65e4d6
2 changed files with 31 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
/** /**
* Friends list functions * Friends list functions
@ -32,6 +33,9 @@ public class FriendsListHelper {
private FriendsListDbHelper fdbHelper; private FriendsListDbHelper fdbHelper;
private Context mContext; private Context mContext;
//Friends list access lock
public static ReentrantLock ListAccessLock = new ReentrantLock();
/** /**
* Public constructor * Public constructor
* *
@ -56,10 +60,22 @@ public class FriendsListHelper {
/** /**
* Get and return the friends list * Get and return the friends list
* *
* @return The list of firned * @return The list of friends
*/ */
public ArrayList<Friend> get(){ public ArrayList<Friend> get() {
return fdbHelper.get_list();
//Acquire friends list lock
FriendsListHelper.ListAccessLock.lock();
//Fetch the list
ArrayList<Friend> list;
try {
list = fdbHelper.get_list();
} finally {
FriendsListHelper.ListAccessLock.unlock();
}
return list;
} }
/** /**

View File

@ -47,9 +47,18 @@ public class FriendRefreshLoopRunnable implements Runnable {
//Get the latest version of the list //Get the latest version of the list
ArrayList<Friend> friendsList = friendsListHelper.download(); ArrayList<Friend> friendsList = friendsListHelper.download();
//Save it (only in case of success) //Acquire a lock over the friend list
if(friendsList != null) FriendsListHelper.ListAccessLock.lock();
friendsDBHelper.update_list(friendsList);
try {
//Save it (only in case of success)
if (friendsList != null)
friendsDBHelper.update_list(friendsList);
} finally {
//Release the lock
FriendsListHelper.ListAccessLock.unlock();
}
try { try {
object.wait(500); //TODO: increase the value object.wait(500); //TODO: increase the value