IOS: Best Way to Cache/Store Strings Persistently - ios

My IOS App periodically collects data that is time and location stamped (from GPS). That data is sent to a server if the app is online at the time the data is collected. BUT, if a data connection is not available I want the data cached to the iPhone/iPad until a data connection is obtained. When the connection is restored the data is uploaded and the cache is cleared. The data is in the form of an array of strings of SQL. Typically the array might contain a dozen or few dozen strings--nothing too large. The data should be persistent until it is cleared even if the app or device is shut down. Suggestions?

Have you thought about using NSUserDefaults for small amounts of data it is a value, key map solution and very simple. For large amounts of data something like JSON is good. Simply make the JSON when the data is created and then when connection in the app is detected parse the JSON and send it to the server.

I think the best way is to work with CoreData or you store the values in a local file.

Related

Determining when it is the most appropriate time to delete a cached object from core data

I have an iOS app written in Swift that requests data from a server, the server responds with the requested data in JSON format and the app serializes that data into a Core Data object to cache it.
The point of this being that the client doesn't have to keep re-requesting the data from the server. It's worth noting that this data could be around 300 KB - 1 MB because it has the potential to contain alot of text and an image.
The data can easily be re-requested from the server if needed. So my question is how would I determine when it is an appropriate time to delete the object from Core Data so that the app doesn't keep storing more and more until the whole device HDD is full (or just generally using lots of HDD space)? What is the general strategy in a situation like this?
EDIT - For more context on how the app works. Users can search for data which is displayed in a UICollectionView. The server responds to the search query with an array of IDs of the relevant database objects and then the client will request them individually when the UICollectionView needs to display the data. When the client makes a request it first checks if its in memory, if not then checks core data and finally requests the data from the server if its not in core data. If it is pulled from core data then the client will tell the server the objects id and the version it has and the server will respond by either saying it's up-to-date or with the relevant updated information. I expect that most objects will never be updated but there is always a possibility

Storing data for drawing graphs in iOS app

I'm creating an iOS app that draws graphs. What's the easiest way of storing the data for drawing these graphs? I'm using GET requests to retrieve data from a server. The data is updated roughly every 15 mins. I need to be able to add the new data every 15 mins to the old data.
I've looked at using Core Data but not sure how to proceed as it appears there are multiple routes I can choose.
Some general pointers would be helpful.
If you need the data to be persisted across runs of the app and have "unlimited" amount then you will want to use Core Data. If you just need a little data during 1 run of the app you can use things like an NSArray of NSDictionary's.
The very very simplest way to save your data is as a plist. NSArray and NSDictionary both provide -writeToFile:atomically: and -initWithContentsOfFile:.
For any complicated retrieval or searching, you'll want to learn about and use Core Data. But for simply dumping the entire array out and getting it back, these methods suffice. You'll encounter memory pressure for large datasets, which is why Core Data has been suggested.
When you receive graph data from server, do you receive delta change or complete set again?
In case its delta change, you should use NSMutableArray or NSMutableDictionary. Keep appending more records.
In case you receive full set, you should clear all objects in NSArray/NSDictionary and re populate with data received on server.
Core data is for persistent storage in file system. Based on your requirement it doesn't look like you need one.

How is data loaded from a remote database stored locally (in memory)

What is the best way that data loaded from a remote database can be stored locally on iOS. (You don't need to provide any code, I just want want to know the best way conceptually.)
For instance, take Twitter for iOS as an example. When it loads the tweets, does it just pull the tweet data from the remote database and store them in a local database on the iPhone? Or would it be better if the data is just stored locally as an array of objects or something similar?
See, I'm figuring that to be able to use/display the data from the remote database (for instance, in a dynamic table view), the data would have to be stored as objects anyway so maybe they should just be stored as objects in an array. However, when researching this, I did see a lot of articles about local databases, so I was thinking maybe its more efficient to load the remote data as a table and store it in a local database and use data directly from the local table to display the data or something similar.
Which one would require more overhead: storing the data as an array of Tweet objects or as a local database of tweets?
What do you think would be the best way of storing remote data locally (in memory) (for an app that loads data similar to how Twitter for iOS)?
I suppose this begs this prerequisite question: when data from a remote database is downloaded, is it usually loaded as a database table (result set) and therefore stored as one?
Thanks!
While it's very easy the put the fetched data right into your array and use it from there, it is likely that you would be benefitted by using a local database for two main reasons: scalability and persistance.
If you are hoping to download and display a large amount of data, it may be unsafe to try to store it in memory all at once. It would be more scalable to download whatever data you need, store it in a local database, and then fetch only the relevant objects you need to display.
If you download the data and only store it in an array, that data will have to be re-fetched from the remote database and re-parsed on next load of your app/view controller/etc before anything can be displayed. Instead, create a local database in which to store the downloaded data, allowing it to be readily available to display on next load while new data is fetched from your remote source.
While there is some initial overhead incurred in creating your database, the scalability and persistance that provides you is more than enough to warrant that. Storing the state of a remote database in a local database is an extremely common practice.
However, if you do not mind the user having to wait for this data to be fetched on every load, or the amount of data being fetched is relatively low or the data is incredibly simple, you will save time and effort on skipping the local database.

iOS app with remote server - I don't need data to persist on app, should I still use CoreData?

Design question:
My app talks to a server. Json data being sent/received.
Data on server is always changing, and I want users to see most current data, not stored/cached data. So I require a user to be logged in order to use the app, and care not to persist data in the app.
Should I still use CoreData and map it to Json's.?
Or can I just create custom model classes and map Json's to it's properties, and have nsarray properties, which point to its child objects, etc. ?
Which is better?
Thanks
If you dont want to persist data, I personally think core data would be overkill for this application
Core Data is really for local persistance. If the data was not changing so often and you didnt want them to have to get an updated data everytime the user visited the page, then you would load the JSON and store it locally using CoreData.
Use plain old objective-c objects for now. It's not hard to switch to Core Data in future, but once you've done so it gets a lot harder to change your schema.
That depends on what your needs are.
If you need the app to work offline, you need to store your information somehow in the client.
In order to save on network usage, you could store locally, then query the server to see if it had an updated answer -- you could do this by sending a time stamp to the server and return a 304 Not Modified if the entity hasn't changed.
Generally, it depends on how much time you have to put into the app and what your specific requirements are, but as a general rule I would optimise for as low bandwidth usage as possible, as that not only reduces potential data costs, but also means the answers will be more quickly available to your users (when online and they have not changed) and also available offline.
If you do not wish to store data locally at all,

iOS Saving Web Service Data to Device

My situation is that I have a universal app that talks to an sql database via odata. When the user retrieves data over the line I want to save that to the device so that if the user stops the app or the app crashes than I can rehydrate the saved device data and we will not have to re-retrieve the data when the app starts again.
My question is for this sitatuation is it more beneficial to user coredata to save the data to an sqllite db or should I save the data to the documents directory? The data can be serialized into an NSData object which could be saved straight to the device from what I have read, where as saving NSData objects to sqllite is not what it is designed for.
Im looking for the most performant of the two options and also the option that will not restrict as much on size restrictions.
Looking forward to any advice that you can give me.
Thanks in advance
If the size of the data is small enough to fit in memory with no problems, then you will probably get the best performance from serializing an NSData object.
If, however, the data reaches the point where it strains memory usage, you will want to use something like Core Data or sqlite to persist it to the disk and only load objects in memory you are using at the moment.

Resources