1

I have Cordova app with plugin for local notification. Problem is that cancelAll() does not wait until it is finished so it is still deleting while I am adding my new messages.

How do I synchronize threads to make cancelAll() wait?

Some related code AlarmHelper::cancelAll()

/**
 * @see LocalNotification#cancelAllNotifications()
 */
public boolean cancelAll(SharedPreferences alarmSettings) {
    final Map<String, ?> allAlarms = alarmSettings.getAll();
    final Set<String> alarmIds = allAlarms.keySet();

    for (String alarmId : alarmIds) {
        Log.d(LocalNotification.PLUGIN_NAME, "Canceling notification with id: " + alarmId);
        String alarmInt = alarmId;
        cancelAlarm(alarmInt);
    }

    return true;
}
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180

1 Answers1

0

How do I synchronize threads to make cancelAll() wait?

You can't. cancelAll() is asynchronous.

Problem is that cancelAll() does not wait until it is finished so it is still deleting while I am adding my new messages.

Why are you calling cancelAll() in the first place? Update your notification in-place.

Also, please do not have multiple notifications, as that is considered to be poor form. Have one notification for all the outstanding "messages", which is cleared when the user has acted upon those "messages" (e.g., visited the inbox).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • There are only one or two messages shown to the user the others are scheduled appear in the future. Sometimes those future dates change and need to be deleted. From JavaScript I do not have access to real list so it is easier for me to delete and recreate them all. Anyway, I don't believe that is not possible. In xcode it was easy to solve a [similar problem](http://stackoverflow.com/questions/14731111/only-first-object-of-nsmutablearray-is-stored-in-nsuserdefaults). – PiTheNumber Feb 19 '13 at 15:55
  • @PiTheNumber: "Anyway, I don't believe that is not possible" -- you are also welcome to believe that the world is flat. Notifications are handled in an OS process, asynchronously from your requests to display or remove them, and nothing you can do will change that short of creating your own version of Android. – CommonsWare Feb 19 '13 at 16:00
  • A multithread language where you can synchronize resource access does not really make sense. Yes, the resource is handled by the OS but I can synchronize my layer. Something like [ReentrantLock](http://stackoverflow.com/questions/3392139/thread-synchronization-java) or [Synchronized Methods](http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html) might work... – PiTheNumber Feb 19 '13 at 16:30
  • @PiTheNumber: "I can synchronize my layer" -- not in any way that would address your problem. You wrote "it is still deleting while I am adding my new messages". The "deleting" is being done by another process, and you have no means of synchronizing anything with that process' work, unless you rewrite that process via your own version of Android on your own ROM mod. – CommonsWare Feb 19 '13 at 16:34
  • No, I can tell from the log that it is my code (posted above) that is running. I only need to synchronize `AlarmHelper::addAlarm()` to wait until `AlarmHelper::cancelAll()` is completed and it will be fine. – PiTheNumber Feb 19 '13 at 16:40
  • @PiTheNumber: If by "wait until AlarmHelper::cancelAll() is completed" you mean "wait until `cancelAll()` on `NotificationManager`" returns, you can synchronize on that, but it would not appear to solve any problem of yours. If by "wait until AlarmHelper::cancelAll() is completed", you mean "wait until the notifications are actually removed", you cannot do what you want, as I have explained repeatedly. – CommonsWare Feb 19 '13 at 17:14
  • Ok, I totally agree with you it would be better to extend the Cordova Plugin so that I could access the stored notifications and then implement the needed management to update and cancel each message individually instead of a cancelAll. Unfortunately, no one is paying me for this. I still believe this will most likely not cause any trouble in my special case. Thank you for your helpful input. Even if I argue with you I still appreciate it a lot! – PiTheNumber Feb 20 '13 at 08:54