Closed bdbehrho closed 5 years ago
got same issue , subscribe function receive multiple same values even one thing changed. I expect to listen only one value by one change on the firebase
Yeah, I can confirm this behavior with the following unit test:
[TestMethod]
public void AddedItemShouldBeAppendedToCollection()
{
var cache = new FirebaseCache<List<long>>();
var original = @"[1, 2, 3]";
var incoming = @"[1, 2, 3, 4]";
var expectation = new []
{
new FirebaseObject<List<long>>("updates", new List<long>() { 1, 2, 3, 4 })
};
cache.PushData("updates/", original).ToList();
var entities = cache.PushData("updates/", incoming).ToList();
entities.Should().BeEquivalentTo(expectation);
}
I initially thought this would be covered in this fix, but that's for observing an item that contains a list, while this particular use case is observing a list directly. This line in FirebaseCache.cs JsonConvert.PopulateObject(data, obj, this.serializerSettings);
appends to the list instead of replacing it.
I did end up getting the correct behavior by making sure collections (anything that implements IEnumerable) are assigned via the primitiveObjSetter rather than JsonConvert.PopulateObject. I do this by adding an extra condition in the if statement:
if ((valueType.GetTypeInfo().IsPrimitive || valueType == typeof(string) || typeof(IEnumerable).IsAssignableFrom(valueType)) && primitiveObjSetter != null)
And as a side note, I don't see the purpose of this line this.dictionary[pathElements[0]] = this.dictionary[pathElements[0]];
(just assigning to itself??) so I removed that line too. I'll make a PR soon.
I have an Object with a list of integers. I want to listen to the list so I can see when a value is added. The Updates come in at the right time, but every time the list receives an update, it has duplicated all of the values.
(Initial setup, list exists and has 10 values)
Example Code:
It is worth noting that the values are not being duplicated in firebase, only the returned values passed into the Subscribe Action. Seems like a bug, but possible I'm just misusing the library.
Thanks for your time! Overall a huge fan of this project.