chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 557 forks source link

Fixed java.util.ConcurrentModificationException #231

Closed Chuckytuh closed 7 years ago

Chuckytuh commented 8 years ago

Calling remove() on the ArrayList while iterating over it throws java.util.ConcurrentModificationException.

didlich commented 7 years ago

+1

I can confirm this BUG in version 0.6.4. Here is a sample code I used to test it. Sometimes if unregisterNdef() is called the java.util.ConcurrentModificationException is thrown, but not always. After app restart unregisterNdef() is working as expected. I can't reproduce it!

function register() {
      ionic.Platform.ready(function () {
        console.log("Register");
        registerNdef();
      });
    }

    function unregister() {
      ionic.Platform.ready(function () {
        console.log("Un-Register");
        unregisterNdef();
      });
    }

    function ndefCallback(nfcEvent) {
      var tag = nfcEvent.tag,
        ndefMessage = tag.ndefMessage;

      // dump the raw json of the message
      // note: real code will need to decode
      // the payload from each record
      alert(JSON.stringify(ndefMessage));

      // assuming the first record in the message has
      // a payload that can be converted to a string.
      alert(nfc.bytesToString(ndefMessage[0].payload).substring(3));
    }

    function registerNdef() {
      // Read NDEF formatted NFC Tags
      nfc.addNdefListener(
        ndefCallback,
        function () { // success callback
          console.log("Registered NDEF listener");
        },
        function (error) { // error callback
          console.log("Error register NDEF listener " + JSON.stringify(error));
        }
      );
    }

    function unregisterNdef() {
      nfc.removeNdefListener(
        ndefCallback,
        function () { // success callback
          console.log("Unregistered NDEF listener");
          alert("Waiting for NDEF tag");
        },
        function (error) { // error callback
          console.log("Error unregister NDEF listener " + JSON.stringify(error));
        }
      );
    }
didlich commented 7 years ago

@don please could you look over the PR, @Chuckytuh is right with his implementation, see: http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re

don commented 7 years ago

@didlich I'll review this soon