Start a new topic
Answered

Observables calling onNext twice for a single data row

Hi, 

I'm using the latest phonegap library and trying to use the new 
observables structure. 

I'm not sure if I have found a bug or if I am using it incorrectly. 

I have a single row in a collection and I'm trying to get this using 
stream.subscribe , however It appears to process this twice, onNext 
appears to be called twice even though there is only a single row in the 
collection, followed by onComplete. 

Code: 

var dataStore = Kinvey.DataStore.collection('favourites'); 
var stream = dataStore.find(); 
stream.subscribe(function onNext(entities) { 
entities.forEach(function(entity,index){ 
console.log("Favourite Processed "+entity.name+" 
"+entity._id); 
}); 
}, function onError(error) { 
console.log("Error getting Favourites "+error); 
}, function onComplete() { 
console.log("Favourites Complete"); 
}); 

Any help appreciated! 

Cheers, 

Aidan W 


Best Answer

John,


If you don't set the datatstore type then the default datastore is a 'cache store' and you are receiving the first response from the cache which is followed by a response from the backend. This is working as expected. The Cache Store executes all CRUD requests against local storage as well as the backend. For a dataStore of type Cache, data is read first from the local cache. After data is read from the cache, a request is made to the backend. The data in the response is saved to the local cache and returned to the user. Any data retrieved from backend is stored in the cache. This allows the app to work offline by fetching data that has been cached from past usage. For more information on data reads, please review this link.


If you want records to be fetched only one time then please try changing the datastore type to 'sync' or 'network'. Please check this link for better understanding.


Thanks,
Pranav


OK.  Thank you.  

Answer

John,


If you don't set the datatstore type then the default datastore is a 'cache store' and you are receiving the first response from the cache which is followed by a response from the backend. This is working as expected. The Cache Store executes all CRUD requests against local storage as well as the backend. For a dataStore of type Cache, data is read first from the local cache. After data is read from the cache, a request is made to the backend. The data in the response is saved to the local cache and returned to the user. Any data retrieved from backend is stored in the cache. This allows the app to work offline by fetching data that has been cached from past usage. For more information on data reads, please review this link.


If you want records to be fetched only one time then please try changing the datastore type to 'sync' or 'network'. Please check this link for better understanding.


Thanks,
Pranav


1 person likes this

I am experiencing the same issue.  Do you have a solution?  

Aidan,

I am looking into it and will get back to you once I have more information to share.

Thanks,
Pranav
Kinvey

 


1 person likes this

Hi Billy,


I may have replied via email this morning before then finding a whole section of the new documentation I had missed to do with DataStore types. This has cleared up a lot of the questions I asked in the email.


One still exists, If I am using cache store the documentation mentions "The library is smart enough to determine when to serve data from the cache and when to fetch it from the network." so why do I receive data from both the cache AND the network? Shouldn't I just receive data from the cache as long as it hasn't been updated (or if I'm offline)?


Currently if I am receiving the same row of data from both Cache AND Network, looking at the object there appears to be no way of telling which is from where (other than possibly order received), is it "Assumed" that I would compare all results returned manually and then ignore duplicates returned via the Network?


As far as I can see, using cached mode, I cannot simply use the observable to return a single copy of each row unless I manually store and compare every result to make sure I do not have duplicates, as it is returning cached and network regardless of whether or not there are local changes or not.


Surely this isn't the case? Unless there is a way of determining if an object/entity has been sourced from cache or network?


Cheers,


Aidan


1 person likes this

Hello Aidan,


The double callback that you are seeing is not related to observables. It is happening because the default datastore is a cache store and you are receiving the first response from the cache which is followed by a response from the backend. 


Please let us know if this helps you move past this issue or if you are still having problems.


Regards,


Billy Gee

Login or Signup to post a comment