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
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 am working on app where I have country object and I will be having around 200 instances of object. Each object will have few properties like flagName, capitalCity etc.. I started working on prototype using only 10 countries and I stored these objects inside the viewDidLoad method as an arrays. I know its not a good practice to "hard code" everything in the source code. In a real app, I would like to externalize these static items and put them in a file or database or somewhere else. What is the best way to store such information ? I am thinking of property list but if the data-model changes then I will have to re-submit the app. Any help would be greatly appreciated. Thanks
Try to load a JSON file from your web site. Create a simple JSON model, hide the file on your web site and have the app load it from there. If you want to change it, you just have to change the JSON file on your server. This way you won't have to re-submit the app.
Cache the content of the file in your app so it can work off line, if needed.
Just a suggestion.
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
I'm planning out a sort of reference book application. For each topic there will be a page with an image and text stored. I don't want to create new views in xcode for each page since there are 100+ topics, I'd rather find the easiest way to store the items in a database and then call the content to display on a view template when the user selects the topic from a list. After searching around I see that this is potentially done with Core Data or SQLite, and maybe even json, but I have not encountered a clear answer.
What's the best way to handle this sort of data?
You should create a database in Core Data and where you'd like to store images, use the response from this tutorial (conversion to Swift is left as an exercise for the reader) and store the fileName as a string.
Don't use json to store 100+ items, it will be very slow. SQL is quite fast, even though it's a mobile device.
I was wondering whether it was possible to import static information, in my case an image, title and a short description - per item, in a UIViewControlller dynamically?
I'm (trying to) create an app that, for the current UIViewController I'm working on, shows the cooperating partners from the company in a list view. Each of them is shown as an item, containing an image (logo), the company name below and below that a paragraph of information about the company.
I was thinking of something like importing an XML feed (no experience with),
such as
<object>
<img src="/images/logo1.png">
<title="Lorem ipsum>
<description="lalalalalalalala"
</object>
<object>
<img src="/images/logo2.png">
<title="Lorem second one>
<description="lalalalalalalala"
</object>
Something like this mockup
By simply importing such thing, I don't have to make up the whole page, calculate how long the UIScrollView would have to be and don't have to adjust it every time a partner gets added or removed. And it might be quite more clear than a whole view controller.
You have to divide the task into smaller steps.
Read the data from an XML file using NSXMParser.
Show the data in a UICollectionView.
The XML part is quite easy. There are many tutorials on the Internet that cover this. For example this one:
Parsing XML data with NSXMLParser
Second step can be more complicated depending on what kind of a layout you want to achieve. If it is a standard flow layout, the implementation will be fairly simple. Again, there is dozens of tutorials on setting up a UICollectionView. Example:
Beginning UICollectionView
Alternatively, you can use UITableView to show your data. In this case, the implementation would be simpler than for UICollectionView.
More reading:
Collection View Programming Guide for iOS
Event-Driven XML Programming Guide
Depending on where your XML feed/document is coming from. If you're just manually creating the document, and shipping it along with each version of your app, then all you need to worry about is getting the info from it; take a look at this tutorial, it describes some info-getting libraries. Take a look at UIImage for info on loading an image from a file. If you instead mean that you want to get information from some kind of remote, it seems like you have to accomplish three things:
save a document including this various static info, so that it persists between app launches,
load from that document to display the saved information, and
keep this document up-to-date with some remote server (?) whence the most current version originates, in perhaps XML format.
First of all, let us assume that inside your application proper, you're going to have the data you want stored as some kind of NSObject. Check out this tutorial on how to save documents with your iPhone app. It explains fairly clearly how to take something like an array or dictionary (OBJC object) and store it. In the end, because your app is sandboxed you may not need to worry about the format in which it is stored. Whenever your application launches, and decides that it has current info, all it needs to do is load from that archived object and you're good to go. If you have some confusion about displaying the information in the UIView itself, refer to Apple's documentation.
You'll probably want to have a separate class altogether that handles keeping the data updated. This depends on your server's implementation. For example, your class could query your server for its info, determine with a hash whether it needs to get new data, and if so, pull down new data; use NSXMLParser as linked to above in order to parse the data, then save with the above method.
EDIT: regarding your worry about having to redesign the UIViewController every time you load, look into auto layout. You basically can create a nib file for your controller, tell it how everything will be in relation to everything else, then just assign values (i.e., images and strings) to all the fields and pow, you've got a consistent design.