Search in sources :

Example 1 with MissingRecipientException

use of org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException in project Signal-Android by WhisperSystems.

the class LiveRecipientCache method addToCache.

/**
 * Adds a recipient to the cache if we don't have an entry. This will also update a cache entry
 * if the provided recipient is resolved, or if the existing cache entry is unresolved.
 *
 * If the recipient you add is unresolved, this will enqueue a resolve on a background thread.
 */
@AnyThread
public void addToCache(@NonNull Collection<Recipient> newRecipients) {
    newRecipients.stream().filter(this::isValidForCache).forEach(recipient -> {
        LiveRecipient live;
        boolean needsResolve;
        synchronized (recipients) {
            live = recipients.get(recipient.getId());
            if (live == null) {
                live = new LiveRecipient(context, recipient);
                recipients.put(recipient.getId(), live);
                needsResolve = recipient.isResolving();
            } else if (live.get().isResolving() || !recipient.isResolving()) {
                live.set(recipient);
                needsResolve = recipient.isResolving();
            } else {
                needsResolve = false;
            }
        }
        if (needsResolve) {
            LiveRecipient toResolve = live;
            MissingRecipientException prettyStackTraceError = new MissingRecipientException(toResolve.getId());
            resolveExecutor.execute(() -> {
                try {
                    toResolve.resolve();
                } catch (MissingRecipientException e) {
                    throw prettyStackTraceError;
                }
            });
        }
    });
}
Also used : MissingRecipientException(org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException) AnyThread(androidx.annotation.AnyThread)

Example 2 with MissingRecipientException

use of org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException in project Signal-Android by signalapp.

the class LiveRecipientCache method addToCache.

/**
 * Adds a recipient to the cache if we don't have an entry. This will also update a cache entry
 * if the provided recipient is resolved, or if the existing cache entry is unresolved.
 *
 * If the recipient you add is unresolved, this will enqueue a resolve on a background thread.
 */
@AnyThread
public void addToCache(@NonNull Collection<Recipient> newRecipients) {
    newRecipients.stream().filter(this::isValidForCache).forEach(recipient -> {
        LiveRecipient live;
        boolean needsResolve;
        synchronized (recipients) {
            live = recipients.get(recipient.getId());
            if (live == null) {
                live = new LiveRecipient(context, recipient);
                recipients.put(recipient.getId(), live);
                needsResolve = recipient.isResolving();
            } else if (live.get().isResolving() || !recipient.isResolving()) {
                live.set(recipient);
                needsResolve = recipient.isResolving();
            } else {
                needsResolve = false;
            }
        }
        if (needsResolve) {
            LiveRecipient toResolve = live;
            MissingRecipientException prettyStackTraceError = new MissingRecipientException(toResolve.getId());
            resolveExecutor.execute(() -> {
                try {
                    toResolve.resolve();
                } catch (MissingRecipientException e) {
                    throw prettyStackTraceError;
                }
            });
        }
    });
}
Also used : MissingRecipientException(org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException) AnyThread(androidx.annotation.AnyThread)

Aggregations

AnyThread (androidx.annotation.AnyThread)2 MissingRecipientException (org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException)2