As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
Observables calling onNext twice for a single data row
A
Aidan Wardman
started a topic
over 6 years ago
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
P
Pranav J
said
almost 6 years ago
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.
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
A
Aidan Wardman
said
over 6 years ago
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
P
Pranav J
said
over 6 years ago
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
John Smith
said
almost 6 years ago
I am experiencing the same issue. Do you have a solution?
P
Pranav J
said
almost 6 years ago
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.
Aidan Wardman
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
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
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstBilly Gee
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
Aidan Wardman
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
Pranav J
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
John Smith
I am experiencing the same issue. Do you have a solution?
Pranav J
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
John Smith
OK. Thank you.
-
How do I use Kinvey in my web app?
-
Is it safe to include keys/secrets in my client-side JavaScript app?
-
Why is the activeUser null even though I am logged in?
-
Login does not work even though credentials are valid.
-
Social login doesn’t work.
-
Appending objects to an Array (HTML5 - JS)
-
New to node.js - need bootstrap to downloadfiles from Kinvey
-
Problem with Aggregation/Grouping
-
Internal Server Error using Twitter Sign Up
-
Data Store
See all 315 topics