Skip to content

Commit

Permalink
Fix logic for multiple sync set names per sync request
Browse files Browse the repository at this point in the history
  • Loading branch information
yukuku committed May 10, 2024
1 parent 7bfa1de commit 37434df
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions Alkitab/src/main/java/yuku/alkitab/base/sync/Sync.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.Call;
import okhttp3.FormBody;
Expand Down Expand Up @@ -202,7 +199,6 @@ public static <C> boolean entitiesEqual(@NonNull final List<Entity<C>> a_, @NonN
}

private static final ArrayMap<String, AtomicInteger> syncUpdatesOngoingCounters = new ArrayMap<>();
private static final ScheduledExecutorService syncExecutor = Executors.newSingleThreadScheduledExecutor();
private static final ConcurrentLinkedQueue<String> syncSetNameQueue = new ConcurrentLinkedQueue<>();

/**
Expand Down Expand Up @@ -241,32 +237,30 @@ public static synchronized void notifySyncNeeded(final String... syncSetNames) {
}
}

syncExecutor.schedule(() -> {
final List<String> extraSyncSetNames = new ArrayList<>();
synchronized (syncSetNameQueue) {
while (true) {
final List<String> extraSyncSetNames = new ArrayList<>();
synchronized (syncSetNameQueue) {
final String extraSyncSetName = syncSetNameQueue.poll();
if (extraSyncSetName == null) {
break;
}
extraSyncSetNames.add(extraSyncSetName);
final String extraSyncSetName = syncSetNameQueue.poll();
if (extraSyncSetName == null) {
break;
}
extraSyncSetNames.add(extraSyncSetName);
}
}

final Account account = SyncUtils.getOrCreateSyncAccount();
final String authority = App.context.getString(R.string.sync_provider_authority);
final Account account = SyncUtils.getOrCreateSyncAccount();
final String authority = App.context.getString(R.string.sync_provider_authority);

// make sure sync is enabled.
final boolean syncAutomatically = ContentResolver.getSyncAutomatically(account, authority);
if (!syncAutomatically) {
ContentResolver.setSyncAutomatically(account, authority, true);
}
// make sure sync is enabled.
final boolean syncAutomatically = ContentResolver.getSyncAutomatically(account, authority);
if (!syncAutomatically) {
ContentResolver.setSyncAutomatically(account, authority, true);
}

// request sync.
final Bundle extras = new Bundle();
extras.putString(SyncAdapter.EXTRA_SYNC_SET_NAMES, App.getDefaultGson().toJson(extraSyncSetNames));
ContentResolver.requestSync(account, authority, extras);
}
}, 5, TimeUnit.SECONDS);
// request sync.
final Bundle extras = new Bundle();
extras.putString(SyncAdapter.EXTRA_SYNC_SET_NAMES, App.getDefaultGson().toJson(extraSyncSetNames));
ContentResolver.requestSync(account, authority, extras);
}

/**
Expand Down

0 comments on commit 37434df

Please sign in to comment.