Confused about parse local datastore & cache - ios

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!

Related

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

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.

Using database offline, then updating when new connection established with iPhone

I have been requested to make my app available off-line, which means storing the data collected via Api for use when no connection available. The problem is that when a new connection is made my local data may be out of date. Also, any changes made while off-line will need to update the server.
I'm aware of a method of syncing databases so that when new connection is made the data is automatically updated both ways. However, after browsing Google I've not found a definitive method of doing this.
Can anyone help point me in the right direction?
There should be a field like a time stamp to indicate last synced time. When ever connection is online go for a fetch validate against the timestamp and update the data in offline storage.
The same way, when you have updates while offline you can set some bool value to check whether data is synced or not and sync when you are online.

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.

How to persist Firebase objects to disk in iOS?

It seems that Firebase iOS implementation doesn't support offline caching of the client model. What this means in practice that:
For Firebase apps requiring an authentication, you need to first authenticate and wait Firebase finish the login (check the user identity, open a socket, etc.) before you can start moving the data. This will require 1-8 seconds (usually 2-5) depending on the network conditions, at least here in Finland.
After authenticating, Firebase first downloads the initial set of data and initializes the client cache. The time to perform this depends on the size of the data you add listeners for, but it's usually quite fast.
The problem here is that if you're using Firebase to implement, for example a messaging app, you'd most likely want to show the user a previously cached version of the message threads and messages, before the actual connection with the backend server is established.
I'd assume the correct implementation for this would need to handle:
The client-side model <-> Firebase JSON mapping (I use Mantle for
this)
Persisting the client-side model to disk (manual implementation using NSKeyedArchiver, or Core Data or such?)
Synchronizing the on-disk model with the Firebase-linked model in memory, when the connection is available (manual implementation?)
Has anyone come up with a solution (own or 3rd party) to achieve 2) and 3)?
It seems Firebase has solved this problem since this question was asked. There are a lot of resources on Offline Capabilities now with Firebase, including disk persistence.
For me, turning on persistence was as simple as the following in my AppDelegate:
Firebase.defaultConfig().persistenceEnabled = true
Assuming your app has been run with an internet connection at least once, this should work well in loading the latest local copy of your data.
There is a beta version of this technology within the client for iOS described here: https://groups.google.com/forum/#!topic/firebase-talk/0evB8s5ELmw give it a go and let the group know how it goes.
Just one line required for persistence with Firebase in iOS
FIRDatabase.database().persistenceEnabled = true
Can be found here in Firebase Docs

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