I actually sizeable amount of data that I retrieve the entire data from Firebase when the user log into the app, or to the different view controllers which require the data from Firebase.
However, I find it meaningless to continuously retrieving the same data as the user navigates through the app. Is there a way for me to save all the data to the phone upon first retrieval after log in and just refer to the local data whenever I need it?
I have used NSUserDefaults for small amount of data but I don't think that it is the right option for my situation.
For these data, I would also require to search them by key when necessary.
You can use any Database such as CoreData, Realm or SQLite(many wrappers available on cocoapods).
You can find difference between the three on on this blog link.
Related
I am starting a new project (learning purposes) and I am trying to figure out what is the best software design pattern to use in the following scenario.
I have several data that need to be downloaded from multiple webservices and store somewhere in my app, to display it later. However each piece of data (e.g. list of teachers, students) will only be used in one or more specific view controllers (e.g. teachersViewController and studentsViewController).
I read that the Singleton pattern or use the AppDelegate to store a variable (an object like ApplicationData) is a bad practise, even more in this example which I want to restrict the data access.
So, which design pattern should I choose? I have read something about dependency injection, but I don't have any clue about it or if it even helps me in this question. If it helps, some examples with explanation would be nice.
You need some sort of database to store downloaded data. Good choices are Realm and Core Data. The right way to process data is:
Check if data is already in DB and show it if available.
Download or update data from server and parse it to objects.
Save objects to DB.
Show data taken from DB to user.
Download data as needed. When you open VC with students then download only students data and so on.
EDITED: If you need all the data on app open then load it and put in a DB before first screen opens. Then just use DB to show data to user.
I have an app where a UITableView is used to represent a friends list. Now, this table is updated by values stored in core data, and I'm downloading friend values to core data via Parse. Instead of accessing Parse to update the tableView directly, I decided to update Core Data and then the tableView because I need the friend information in other parts of the app, and thought it would be more efficient to use Core Data than to have calls to Parse again and again. Everything works fine!
However, my app has a log in system before users can use the app. And when I created another dummy user to test it, I found that the friend values stored in Core Data by my actual account were being used to update the friend list tableView! When actually the tableView should be empty (new user).
I don't know exactly how Core Data works but I figure it uses some segment of the device's memory to store entities. My question is this, is it possible to use Core Data to store private information related to a particular user that can't be accessed by other users that log into the same device? Or should I continue to make calls to Parse whenever I need information?
Hope you guys understood my question, thanks!
iOS is not a multi-user system. Your app stores its files in a sandboxed folder structure and this sandbox is independent of any user logins you have implemented in your app.
When a new user logs in (or, if you prefer, when a user logs out) it is up to you to remove any data you have stored in Core Data that you don't want another user to have access to.
Parse can save data offline by Local Storage or cache the request by Cache Policy
They are much faster than creating your own database (by CoreData).
I have an app similar structured to the new Notes app. A sidebar contains information about different files (date, title, excerpt) and the detail view shows the whole content.
To store data in my app, I use NSKeyedArchiver. Each file is stored seperatly to make iCloud Sync easier.
So here is my question: Is there any good way of storing and loading this data in the sidebar?
The problem is that when the app starts, it iterates through every available file to load the information needed. However, the whole object graph loads for each file even if I only need the date and the title. Is there anything I can do to optimize this?
Should I use a seperatly index file where all dates and titles are stored?
Am I using the wrong storage concept?
Any advice would be appreciated!
Sounds like what you need to do is store the key information (date and title) separately from the rest of the data.
When you start the app, you can load the key information and display it, then retrieve the rest of the data only when required, by linking the title to the content.
You could use CoreData for this - but probably overkill.
Realm will work very well for this. There's a good tutorial available here http://www.raywenderlich.com/112544/realm-tutorial-getting-started
I am taking development courses for ios and I was wondering if I wanted to create a translation app would i use a array list to do so? As example code is:-
var dictionary = [“talofa”: “hello’, “faafetai”: “thank you”]
print(dictionary[“talofa”])
it shows up in the logs as “hello” but there has to be an easier way to do translations otherwise I would be fitting a whole language in a array list?
I also read online that people have been using third party services like google to make a translation app but my language is not on google (Hawaiian) what do I do?
First of all, what you are using in your example, in swift/objc it is called a dictionary.
Secondly, for such a huge amount of data, I recommend you use some sort of persistent storage. You can use plain text to store the dictionary (like creating a .plist file), but being iOS I would recommend setting up coredata.
CoreData will allow you to store the information on the device, and access it through a data Model.
Here you can find an example on storing in a file.
Here you can find an example on storing in CoreData.
I personally recommend using coredata for such a large quantity of data. Plist files are more suitable for storing low information quantities (like saving some credentials, some settings, etc).
You need to use DB for this. You can update it from your server when user will have connection, so you don't need to re-submit your app when you will update your vocabulary.
You can use CoreData as #Alex Bartiş told you or you can try another one which becomes popular: Realm
Can I use this technique, or will it get my app rejected by Apple?
The application starts, downloads some JSON data, and stores it in an array.
A UITableView loads the data from the array.
When the user starts the application again, the application again downloads the JSON data and stores it in the array, and the UITableView again loads the data from the array.
Is this OK, or I should use Core Data to store the data?
Note that some data may change in the JSON, so if I store it using Core Data, it will be difficult for me to track changes and reflect them in Core Data.
or I should use core data to store the data from JSON.
I don't even know what makes to think you'd be obliged to use CoreData if you're using JSON. It's definitely not a must. They are two distinct technologies, with a totally different purpose. For example, using JSON for quick communication to your server is just fine.
PassKit is right, that you should think about what you want the app to do if there's no Internet connection. Do you want the app to crash or just show nothing at all? Probably better, show the user the last known information, perhaps showing them the date and time that it was last refreshed and/or warn them that it might not be current.
To do that, you will want to save the JSON after it's successfully downloaded. You don't need to use Core Data for that (in fact, that's almost certainly overkill), but you probably do want to save it to your Documents folder. You can just save the JSON object to a file using writeToFile. Then, when it tries to retrieve the information from the server at some future date, if it's not found, look for the information in the Documents folder.