aws-mobile-appsync-sdk-ios
aws-mobile-appsync-sdk-ios copied to clipboard
lastSyncTime is updated after first page of results of sync
Is your feature request related to a problem? Please describe. Most real-world GraphQL requests require some form of paging. The SDK is saving the "lastSyncTime" after the initial page is downloaded. The problem is that if there is a network/server/whatever error in the subsequent pages, the data will never be updated until the next full sync.
Describe the solution you'd like Perhaps make AppSyncSubscriptionWithSync.setLastSyncTime public or provide a new function to simply use the current datetime. Or maybe a result code can be returned to from the completion handler.
Describe alternatives you've considered There's not much else I can do other than delete the subscription cache where "lastSynctime" is stored.
Additional context
Hello @airstance
Could you please provide some more info regarding your use-case --
- Are you using the
sync
operation with a paginated query? - Can the app load the paginated data via a separate query and let the sync operation take care of the ongoing data stream?
-
Yes, I'm using paginated results in the sync query.
-
Yes, that's what I"m doing. The sync operation triggers the first page and then I use fetch calls to handle the string of following pages.
The problem is that that string of fetch calls can take a long time to finish. If an error occurs during that time, then the subsequent pages are "lost" until the next base sync occurs. I suppose I can capture that situation by updating my own lastSyncTime and re-creating the base sync object again.
I've just spent several days trying to track down a bug that's related to this issue.
Somehow my cache is being corrupted with a root query for a fetch. The cache object contains all the top-level information (nextPage, limit, etc.) but the query result (in this case "companies") is nil.
As a background, my sync query can result in paginated results so I fetch those pages manually in the sync resultHandler. When the fetch for the corrupted entry occurs, it calls my resultHandler with result=0 and error=0. I can't handle this case elegantly.
Depending on when the last successful sync happened, the sync code will follow up the cache request by doing a server request for either the delta query or the base query.
If it uses a base query, then the cache will be cleaned up when all the pages are requested. If it uses a delta query, then I'll be missing all the data from when the cache error occurred and the delta query.
For now, when I detect this condition, I issue a manual fetch from the server for all pages. Unfortunately, this causes two base queries to be happening in parallel when the sync issues a base query instead of a delta query.
Edit: After a brief investigation, I think the corruption happens because the cache updates don't happen in a transaction and I'm constantly restarting my app while debugging. The records are being flushed in a half-updated state. I'll try adding my my transaction change and see if that makes it go away.
I think the real fix is to expose setLastSyncTime from the delta sync object. That will allow me to reset it when I want to force an earlier time. It also solves the problem when I use clearCache but the lastSyncTime (which lives in the subscription database) doesn't get cleared as well.
Just to follow up, the source of the cache corruption was found and reported as #244.
That still doesn't resolve the issue when some other error or condition would require a full-sync to happen before the specified baseRefreshIntervalInSeconds elapses.
Another solution would be to add the "fetchMore" concept to the iOS client. That would put the burden of managing the active queries for subsequent pages on the appsync client. It could then better manage these conditions.
Thank you for opening this issue. AWS AppSync SDK for iOS entered maintenance mode in September 2023 and will receive no further updates as of September 2024.
Please use Amplify Swift going forward. For information on upgrading to Amplify Swift, refer to the Upgrade from AppSync SDK documentation.