Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.
This repository was archived by the owner on May 19, 2025. It is now read-only.

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

Open

Description

@olivierchatry

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

Activity

  1. olivierchatry commented on Sep 20, 2017

    @olivierchatry
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions