-
Notifications
You must be signed in to change notification settings - Fork 122
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
CacheAndNetwork and offline #16
Comments
Can you share your dart code where you call this query? |
Hi!
Error in
|
Hello guys! Is it bug in ferry or my mistake? |
This should be fixed in Ferry. For now, can you use a E.g. if (response?.loading ?? true) |
If use is check, then not getting error. But if app is offline, then showing CircularProgressIndicator(). Will be cool if when internet is offline, then get data from cache. |
@Feduch so no data is ever returned? That definitely shouldn't be happening. I'll have to investigate futher. |
@smkhalsa no data is returned, if internet offline. In code: |
Thanks. I believe the issue here is that the Link throws an error when offline which cancels the stream. We need to improve error handling more generally (#8), but I'll try to prioritize this. |
@smkhalsa Hi! Yes, thank you! Question, now if app is offline, then do not get data from cache. |
Does "CacheFirst" work as expected for you offline? Also, does "CacheAndNetwork" work for you when you're online? The issue with CacheAndNetwork is that the cached data will get returned immediately (if any exists), then if the network response fails, the stream will emit another response with the Network Error and no data. I think we could simply make a change to use the cached data when there is a Network Error. That seems to be what would be expected, but maybe I'm missing an edge case. |
Yes, it`s work. When app is offline, data get from cache. The database has data that can change at any time, they need to be updated in the application.
Yes, work is very good) |
@Feduch Can you please confirm whether this is still an issue in the latest ferry version? |
@Feduch closing for now. If this is still an issue, feel free to reopen. |
@smkhalsa This might still be an issue. When using
Expected behavior would be for the response to contain cached data even when |
.CacheAndNetwork will return cached data, and then return the result of the network request. You can filter the errors from the UI using logic like this (did not run this, this is just the idea): Stream<OperationResponse><Data,Vars> filterErrorsAfterData<Data, Vars>(Stream<OperationResult<Data,Vars>> inputStream) {
bool isFirst = true;
return inputStream.transform(StreamTransformer.fromHandlers(
handleData: (data, sink) {
if (isFirst) {
sink.add(data);
isFirst = false;
} else if (!data.hasErrors) {
sink.add(data);
}
},
handleDone: (sink) {
sink.close();
},
));
} |
This is great, thank you. I ended up modifying your solution a bit so I can use the full response object. It would be convenient for the cached data to come back with the second network response though but I can see the logic for both cases. import 'dart:async';
import 'package:ferry/ferry.dart';
makeRobust<T, D>(Stream<OperationResponse<T, D>> stream) {
OperationResponse<T, D>? prevResponse = null;
return stream.transform(
StreamTransformer.fromHandlers(
handleData: (response, EventSink<OperationResponse<T, D>> sink) {
if (response.hasErrors &&
prevResponse != null &&
prevResponse!.data != null) {
sink.add(prevResponse!);
} else {
prevResponse = response;
sink.add(response);
}
}, handleDone: (sink) {
sink.close();
}),
);
} |
Hi!
In app I use FetchPolicy.CacheAndNetwork for query.
When I disable internet on phone or internet very bad, but data is set in cache, I get this error.
It is normal logic for CacheAndNetwork?
When I use query without FetchPolicy, data is set in cache and disable internet, it is ok.
The text was updated successfully, but these errors were encountered: