diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java index a6171f7..801a9a3 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/FriendsListHelper.java @@ -14,6 +14,7 @@ import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; +import java.util.concurrent.locks.ReentrantLock; /** * Friends list functions @@ -32,6 +33,9 @@ public class FriendsListHelper { private FriendsListDbHelper fdbHelper; private Context mContext; + //Friends list access lock + public static ReentrantLock ListAccessLock = new ReentrantLock(); + /** * Public constructor * @@ -56,10 +60,22 @@ public class FriendsListHelper { /** * Get and return the friends list * - * @return The list of firned + * @return The list of friends */ - public ArrayList get(){ - return fdbHelper.get_list(); + public ArrayList get() { + + //Acquire friends list lock + FriendsListHelper.ListAccessLock.lock(); + + //Fetch the list + ArrayList list; + try { + list = fdbHelper.get_list(); + } finally { + FriendsListHelper.ListAccessLock.unlock(); + } + + return list; } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java index f2fc740..b46fef9 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java @@ -47,9 +47,18 @@ public class FriendRefreshLoopRunnable implements Runnable { //Get the latest version of the list ArrayList friendsList = friendsListHelper.download(); - //Save it (only in case of success) - if(friendsList != null) - friendsDBHelper.update_list(friendsList); + //Acquire a lock over the friend list + FriendsListHelper.ListAccessLock.lock(); + + try { + //Save it (only in case of success) + if (friendsList != null) + friendsDBHelper.update_list(friendsList); + + } finally { + //Release the lock + FriendsListHelper.ListAccessLock.unlock(); + } try { object.wait(500); //TODO: increase the value