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.
Related
This question is not about the technical problem, but rather the approach.
I know two more or less common approaches to store the data received from the server in your app:
1) Using managers, data holders etc to store the data. They are most often some kind of singleton and are used to store the models received from the server. (E.g. - the array of the posts/places/users) Singletons are needed to be able to access the data from any screen. I think the majority of apps uses this approach.
2) Using Core Data (or maybe Realm) as in-memory storage. This approach avoids having singletons, but, I guess, it is a bit more complex (and crash risky) to maintain and support.
How do you store data and why?
P.S. Any answers would help. But big "thank you" for detailed ones, with reasons.
The reason people opt to use Core Data/Relam/Shark or any other iOS ORM is mainly for the purpose of persisting data between runs of the app.
Currently there are two ways of doing this, for single values and very small (not that I encourage it) objects you can use the UserDefaults to persist between app launches. For a approach closer to a database, infact in the case of Core Data and SharkORM, they are built on top of SQLite, you need to use an ORM.
Using a manager to store an array of a data models will only persist said models for the lifetime of the app. For example when the user force quits the app, restarts their device or in some circumstances when iOS terminates your app, all that data will be lost permanently. This is because it is stored in RAM which is volatile memory, rather than in a database on the disk itself.
Using a database layer even if you don't specifically require persistence between launches can have its advantages though; for instance SharkORM allows you to execute raw SQL queries on your objects if you don't want to use the built in powerful query builder. This can be useful to quickly pull the model you are interested in rather than iterating through a local array.
In reply to your question, how do I store data?
Well, I use a combination of all three. Say for instance I called to an API for some data which I wanted to display there and then to the user, I would use a manager instance with an array to hold the data model.
But on the flipside if I wanted to store that data for later or if I needed to execute a complex query on it, I would store it on disk using Shark.
If however I just wanted to store whether or not the user had seen my on boarding flow I would just persist a boolean value into UserDefaults.
I hope this is detailed enough for you.
CoreData isn't strictly "in-memory". You can load objects into your data model and save them into their context, then they might actually be on disk and out of main memory, and they can easily be brought back via fetch requests.
Singletons, on the other hand, do typically stay in main memory all the time until the user terminates the app. If you have larger objects that you are storing in some data structure (e.g. full resolution images when all you really needed was a thumbnail), this can be quite a resource hog.
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.
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.
I am somewhat new to Core Data and have a general question.
In my current project, users can access data reported by various sensors in each county of my state. Each sensor is represented in a table view which gathers its data from a web service call. Calling the web service could take some time since this app may be used in rural areas with slow wireless connectivity. Furthermore, users will typically only need data from one or two of the state's 55 counties. Each county could have anywhere from 15 to 500 items returned by the web service. Since the sensor names and locations change rarely, I would like the app to cache the data from the web service call to make gathering the list of sensors locations faster (and offer a refresh button for cases where something has changed). The app already uses Core Data to store bookmarked sensor locations, so it is already set up in the app.
My issue is whether to use Core Data to cache the list of sensors, or to use a SqlLite data store. Since there js already a data model in place, I could simply add another entity to the model. However, I am concerned about whether this would introduce unnecessary overhead, or maybe none at all.
Being new to Core Data, it appears that all that is really happening is that objects are serialized and their properties added as fields in a SqlLite DB managed by Core Data. If this is the case, it seems there really would not be any overhead from using the Core Data store already in place.
Can anyone help clear this up for me? Thanks!
it appears that all that is really happening is that objects are
serialized and their properties added as fields in a SqlLite DB
managed by Core Data
You are right about that. Core Data does a lot more, but that's the basic functionality (if you tell it to use a SQLite store, which is what most people do).
As for the number of records you want to store in Core Data, that shouldn't be a problem. I'm working on a Core Data App right now that also stores over 20,000 records in Core Data and I still get very fast fetch times e.g. for auto completion while typing.
Core Data definitely adds some overhead, but if you only have few entites and relationships and are not creating/modifying objects in more than one context, it is negligible.
Being new to Core Data, it appears that all that is really happening is that objects are serialized and their properties added as fields in a SqlLite DB managed by Core Data. If this is the case, it seems there really would not be any overhead from using the Core Data store already in place.
That's not always the case. Core Data hides its storage implementation from the developer. It is sometimes a SQL db, but in other cases it can be a different data storage. If you need a comprehensive guide to CoreData, I recommend this objc.io article.
As #CouchDeveloper noted, CoreData is a disk io/CPU bound process. If you notice performance hits, throw it in a background thread (yes - this is a pretty big headache), but it will always be faster than the average network.
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,