public ArrayList<NotifrySource> syncFromJSONArray( Context context, JSONArray sourceList, String accountName) throws JSONException { ArrayList<NotifrySource> result = new ArrayList<NotifrySource>(); HashSet<Long> seenIds = new HashSet<Long>(); for (int i = 0; i < sourceList.length(); i++) { // See if we can find a local object with that ID. JSONObject object = sourceList.getJSONObject(i); Long serverId = object.getLong("id"); NotifrySource source = NotifrySource.FACTORY.getByServerId(context, serverId); if (source == null) { // We don't have that source locally. Create it. source = new NotifrySource(); source.fromJSONObject(object); // It's only locally enabled if the server has it enabled. source.setLocalEnabled(source.getServerEnabled()); source.setAccountName(accountName); } else { // Server already has it. Assume the server is the most up to // date version. source.fromJSONObject(object); } // Save it in the database. source.save(context); seenIds.add(source.getId()); } // Now, find out the IDs that exist in our database but were not in our // list. // Those have been deleted. ArrayList<NotifrySource> allSources = NotifrySource.FACTORY.listAll(context, accountName); HashSet<Long> allIds = new HashSet<Long>(); for (NotifrySource source : allSources) { allIds.add(source.getId()); } allIds.removeAll(seenIds); for (Long sourceId : allIds) { NotifrySource source = NotifrySource.FACTORY.get(context, sourceId); NotifryMessage.FACTORY.deleteMessagesBySource(context, source, false); source.delete(context); } return result; }