Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteRecord : recordWillDelete actually update the belongTo parent on firebase, causing crash #525

Open
olivierchatry opened this issue Sep 19, 2017 · 1 comment

Comments

@olivierchatry
Copy link
Contributor

olivierchatry commented Sep 19, 2017

Version info

Latest version ( 2.0.8 )

Test case

This is hard to reproduce as it depends on user input, network lag, and direction of the wind.

Steps to reproduce

Delete a record while having user input or heavyload.

Expected behavior

No error on the console

Actual behavior

Error saying that record cannot be deleted because state is updated.

Explaination :

This is a funny one. Basically recordWillDelete ( function called before actual deletion ) will update directly the parent on all belongsTo relation

    recordWillDelete: function recordWillDelete(store, record) {
      var _this2 = this;

      record.eachRelationship(function (key, relationship) {
        if (relationship.kind === 'belongsTo') {
          var parentRecord = record.get(relationship.key);
          var inverseKey = record.inverseFor(relationship.key);
          if (inverseKey && parentRecord.get('id')) {
            var parentRef = _this2._getCollectionRef(inverseKey.type, parentRecord.get('id'));
            _this2._removeHasManyRecord(store, parentRef, inverseKey.name, record.constructor, record.id);
          }
        }
      });
    },
    _removeHasManyRecord: function _removeHasManyRecord(store, parentRef, key, typeClass, id) {
      var relationshipKey = store.serializerFor(typeClass.modelName).keyForRelationship(key);
      var ref = this._getRelationshipRef(parentRef, relationshipKey, id);
      return (0, _emberfireUtilsToPromise['default'])(ref.remove, ref, [], ref.toString());
    },

Since everything is done async, from time to time, the loopback of the parent set value will actually modify the record you are trying to delete ( I guess trying to set the parent to null ), right before deletion, so the state will not be valid anymore.

If I understand correctly, it is anyway the responsability of the user to "save" the parent value on modification, so emberfire should not do what it does ( or at least not modify the record value while in deletion).

Not sure if it is understandable, but what I did was to simply override the willDeleteRecord in the application adapter by that :

	recordWillDelete: function recordWillDelete(store, record) {
		// here we are doing nothing. Emberfire try to update parent for belongTo relationship directly
		// on firebase, which would lead sometimes to an error as the child get updated, while we delete it
		// when the parent receive the loopback from firebase.
	},

and no more issue

@olivierchatry
Copy link
Contributor Author

It is also written in the documentation, that you need to save both side :
https://github.com/firebase/emberfire/blob/master/docs/guide/relationships.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant