What approach to take when saving json locally on ios device? - ios

Currently my app pulls data from a custom api, parses it and saves the data to multiple arrays.
I am using AWS RDS to store all the data which is displayed on the api, and using AWS EC2 to host the file to access the api.
The problem I have ran into is that each download of the api is ~1mb and AWS charges $0.09/GB of data. I need to lower costs and so I can't have my app pulling the api data every time the refresh function is called. (my api updates every 4 hours. If users refresh the app before my api has updated, the refresh function will do nothing).
My current idea to solve this is either:
(1)download the json data onto the device, then parse & save the offline data to arrays
(2)or download and parse it into arrays, then save those arrays locally (from searching I believe I need to use NSKeyedArchiver or UserDefaults?)
I am not sure what the best approach to this is.

Related

Store client-side data, making it available to a service worker

In my node.js chat app, I want to save chat data in offline mode and push notification when online. Basic chat app is done. I'm using ws node module for server code. In client side code, I want to use service worker for storing offline data. I have done some home work https://jakearchibald.com/2014/offline-cookbook/ https://ole.michelsen.dk/blog/making-an-offline-webapp-with-service-workers.html, but I think I don't understand how can I store chat data in service worker offline data.
IndexedDB is a general purpose database that's available client-side in both the service worker and main web page context. I'd suggest using that to store data like chat messages.
There are a number of libraries out there that wrap IndexedDB to make the interface friendlier. A relatively newer one, idb, has the advantage of providing a promise-based interface, which comes in handy when using IndexedDB from a service worker, since so much of the asynchronous code there is already promise-based.
Once I also have to faced that type of problem. So what I do, is some kind of cheating. I create an object in IndexedDB where I store every chat data temporally and after successfully send my chat data to server, I immediately delete my IndexedDB data. If chat data failed to send to server, it cannot delete temporally IndexedDB data. And when server is available to receive chat data, first check IndexedDB data and send to saved chat data (if any left).
NB: this is not accurate solution, not even close.

RestKit automatically POST/PUT when back online

Our app supports offline activity. Meaning we want to persist locally the creation of new core data objects as well as any modifications on existing objects. Then when the app goes online again we automatically push those changes (and any dependencies) up to the server.
I would think that RestKit would support such an operation, but currently when offline we store creations/modifications in a local cache. If I kill the app, those changes are not persisted. And also there is no attempt by RestKit to post those items to their originally intended endpoints.
I cannot find any documentation to support what we need here.
Is there a way for RestKit to do what we need?
If not, how do I get offline changes to persist to the disk (and not cache)? Then would it be appropriate to flag those as not uploaded to server, and then try uploading them when we are back online?
Any other important things I should consider?
At the time of writing RestKit does not support that feature.
To save to disk you need to call saveToPersistentStore: instead of just save: on the MOC.
You need to implement a scheme yourself, observing the 'online' status of the app and scanning the data store for things that need to be uploaded (which means maintaining a flag to indicate if it's happened yet).
I solved this issue by adding another field called 'updated' to my object. This field is set to true or 1 when the object is created or modified. Each time the application is started or synchronized, it iterates through the local core data copy and sends the objects with 'updated' set. On the web service, the response ALWAYS clears 'updated' to false when returning a response. This works well in the case where the web service and app are both online.

Confused about parse local datastore & cache

I’m developing a iOS App and I want to have a level of offline support and I’m struggling out of local datastore or cache which approach to use as It appears that you can’t use these two feature together.
My query is quite basic and doesn’t change only the data that is retrieved can change.
if i used one of the cache policies, i get connection errors and nothing appears to be returned from the cache.
The workflow i’m after is on the lines of below.
->When connected to the internet perform query and store objects locally.
->if there is no internet retrieve previously downloaded objects.
For the workflow you describe I think you're looking for a cache. If you would like the user could modify the data without connection and then, when there is wifi again, synchronise the local data with the remote data then you'll need the local datastore behavior.
The problem for me is when you want both in different parts of the same app because in parse in you use local datastore you can't use the cache. I don't really understand why!

Fetch large data using multiple requests

My front-end is a Rails application, and my backend is a Padrino application.
I want to fetch a large amount of CSV data from the backend. Doing makes the HTTP request timeout in the browser.
I can not query the backend again and again, because each time it will generate the same data, there is no concept of offset and limit records.
I tried sending directly from the backend to LB but it is not working for me.
To summarize, an array of 10000k rows is generated to be sent to the UI or downloaded in a streaming fashion.

Need help for RestKit data sync scenario

I am using RestKit for an iOS To app. I already had done following using restkit:
1. Pull server objects from rest api in json format.
2. Delete orphan objects in core data which are no longer present on server.
Now i have to build the following scenario, if the internet is available on the device and user is adding a new data item,then what should i do first i.e should i store the new data first locally and then post to server or first i post the data to server and the pull it back on device ?
Secondly if the internet is not available on device and user inserts a new data item then saves data locally, On internet availability how do i post newly added data items to the server i.e what approach should i follow and if restkit can help me tackling this scenario ?
RestKit includes reachability monitoring (actually part of AFNetworking). So you can set a block to be run when the status changes:
[objectManager.HTTPClient setReachabilityStatusChangeBlock:...
Generally, store the item locally in all cases. When the item has been pushed to the server, set the sync date or a flag on the item to confirm that it has been updated.
This is really a broader question about how you manage local modifications and updates to the server. You may want an overall scheme to list the dirty objects and push updates to the server and have the server response set the sync time for each item. If you use 2 dates (one for the last local modification and one for the remote sync) then a quick predicate fetch on the model will tell you which objects are dirty and need to be pushed to the server.

Resources