iOS Core Data search speed improvement - ios

I am struggling improve the search speed of my iOS app which uses core data. Can anyone help or suggest alternative solutions to improve my search speed? I've listed details to my situation below.
Project Details
I am currently creating a data reference app which uses core data with a preloaded SQLite database. I want to be able to search on one or more attributes of an entity which could contain over 100000 records and return results quickly.
The best results I have achieved so far(searching still quiet slow though) is to load a view with a search display controller, set the fetch limit(currently 100) for the fetch request of the fetchResultController. I've also used search scopes to simplify the predicates. I do use the 'contains' keyword in my predicates, but I am not sure how to implement the suggestion in session 137 of WWDC 2010 and what keywords I should be storing or how many I should store.
Here is a link to one of my classes,
http://pastebin.com/cHHicc1s
Thank you for your time and help.
Regards
Jing Jing Tao

You may want to normalize an existing attribute as a new attribute then index it. Remove the "CONTAINS" from your predicate and instead use >= or < etc values. Also, normalize the search text so that the comparison balances. Apple documents all this in the 'Derived Property' example and in WWDC 2010 session # 118 video.

If you are doing large searches on attributes, you should create indexes. You can do this in Xcode when you define the model. Click on the entity, and right under where you specify the entity name, you can create additional indexes.
Note, however, that you will incur additional file size overhead, and inserts/deletes will also take a bit more time. But, your searches will be very fast.

Related

Best way for storing 100+ questions and answers

I am designing an exam practice app that will have the following format, requiring the user to rank the answer 1-5 to sections A-E (using a scroll view) for the same question that will be displayed at the top.
Here is an image:
Each question therefore has 5 parts. I am unsure of what is the best way to store the questions and answers. I read something about plists. Would that be the way to do it? If so, could you recommend any tutorials with images?
Just to clarify, the labels A-E are where the text for the subsections will go and the user will have to rank the appropriateness for each of these.
Thank you!
Few options here:
1. Core Data
Prepopulate .sqlite file with questions and include with app. Keep track of user's progress and attempts and whatever other stats.
This approach also give your ability to label questions by a topic (or any other criteria) and present questions to user that they need or that they failed most.
2 Get data from server
A bit more complicated but offers more benefits. With this approach you would be getting questions in json format.
Benefit of this approach is that you can add any number number of questions and tests without resubmitting your app.
3. Store as text/plist with app
Yes, you can also store your questions as text in plist or json format and as app loads populate core data or keep it in memory to display. The latter approach, however, would offer the least amount of benefit and flexibility to the user.
I suggest using SQLite. See Ray Wenderlich's Tutorial on SQLite for iOS
And there is obviously a not so suitable but easy way and that is using property lists as key and value pairs which i do not recommend.

How to preload data into a database for iOS?

I'm creating an app that manages mileage. There are pre-set miles for each route (there are 28 locations with routes between each, the mileage is set for each).
I am trying to 'preload' a database table with those routes/mileages so that when the user tracks a new route they've gone (say point A to point B) the app can easily figure out how many miles the user traveled with a quick query.
I've done website development and am familiar with mySQL - but there are some GUI's that I use to preload that data (phpmyadmin); there doesn't seem to be anything similar with the iOS that I can find so I can preload the mileage data into a table (entity?) so I can work on creating the interface to work with that data.
How should I go about achieving this? I am hoping to use core data to achieve this if possible - although SQLite seems like another options some have mentioned. But even others mentioned just holding that data in an array. Being so new to iOS programming - I'm just looking for a solid way to accomplish a pre-loaded database table.
Per the comment from Aaron Brager - using this tutorial I have went with Core Data
http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated

caching model(DTO) classes for later use

I am working on an application which uses foursquare and other server api-s for getting information from the internet. But I have to use some datas when the application is not connected to the internet, I need a method which easily saves these datas from the internet store them on the "disk" as a cache if the phone lost connectivity. Basically I want to store some of my model classes like:
VenueCategory contains a name, id, images(~10), weather reports for 7 days, venues.
A Venue contains images, rating, name, category, categoryImage, address, phone number and open hours schedule.
A water report contains date, max, min temperature, wind, ....
I am thinking on 3 methods but I don't know which is the best for my problem, maybe you can give me better ideas.
Database
Pro:
I get a nice representation from my datas.
Cons:
It is hard to modify if the application is live.
I don't need a new table for the venue category, a table is to much for 1 record inside it.
I have to do a lot of query, insertion, deletion, update, etc.
Serialization
It is easy if I can found a nice way I just say write the whole class to disk and read from disk.(I've never tried)
Plist: (just like the database)
My final question is that, what do you think which is the best and why? Do you have better idea?
The simplest way to approach this is (IMO), is to have your DTOs comply to NSCoding and serialize them using NSKeyedArchiver and deserialize them using NSKeyedUnarchiver.
You can use AutoCoding for that, which automatically implements the required methods in NSCoding with no effort at all.
Have you considered using CoreData? That was the first solution that came to my mind.
CoreData is (by default) based on SQLite and is in my opinion the defacto standard for persisting objects in iOS.

iOS Map Kit Locations read from database

I have been tasked with creating an iPhone application for a client.
I have some coding experience but only in C# so it doesn't really help here but other than that I am a complete novice on iPhone coding.
What I am trying to accomplish is to get some form of store locator on a map.
I have successfully added the map, get the user location with it zooming into the user. I have added 2 annotations (Which I believe the the best way to go about showing locations on the map).
I have 2 queries that I need help with, What is the best way to go about listing the stores in some form of database. XML, PList, .sql etc... (this would also need to be read from the web as it would need to be easily edited as new stores would be added a lot). Is it possible to loop through the database and dynamically add the stores onto the map within a location of the user?
I am not asking anyone to write any code for me, I am just asking for some help as I have googled the hell out of this and cant seem to find anything that helps.
Any help would be much appreciated,
Thanks
In terms of your potential formats for saving these locations, you options include:
XML/JSON are good formats for exchanging data with a remote server, but less ideal for a local database (though they theoretically could be used for that purpose). JSON is marginally easier to deal with (using NSJSONSerialization), but XML can be relatively easily parsed, too (using, for example, NSXMLParser). If you're doing network operations, I also heartily recommend looking at AFNetworking, which offers some nice advantages over the standard NSURLConnection. This, of course, presumes that you have written a web service on your server to deliver the necessary JSON or XML feed.
Plist is a fine, simple format if you want to save a short, local list of locations on iOS devices. Saving data to a plist is as simple as calling writeToFile method for your NSDictionary or NSArray and reading data is done via [NSDictionary dictionaryWithContentsOfFile:filename] or [NSArray arrayWithContentsOfFile:filename].
Core Data is a good, iOS-specific format for larger databases. It's probably the preferred iOS mechanism for dealing with persistent objects, but is an order of magnitude more complicated than plists.
SQLite is also a good database format if you're thinking about a structure that lends itself towards larger database, but also which lends itself towards eventual rollout to multiple platforms (e.g. both Android and iOS). If you decide to go SQLite route, consider an Objective-C wrapper (such as FMDB), which will simplify your life greatly.
Implicit in all of the above discussion is that, yes, you certainly can write code that iterates through your database and/or model data structures, extracting the necessary location information, and dynamically add annotations to your map. The Location Awareness Programming Guide should help introduce you to some of the MapKit related features.
"Is it possible to loop through the database and dynamically add the stores onto the map within a location of the user?"
Yes. Just as you have created those first two annotations, you now need to create more annotations in a loop. The only additional info you might need is that once you have added an annotation to the map it will stay there until you remove it. So you don't need to maintain your own list of annotations unless you want to do something else with it. Just fire and forget. So now your question comes down to how to loop through data from your chosen data source in Objective-C and not MapKit specific.
I know this is old but if anyone else comes across this like I did, you can use tmysqlkit by tanmay bakshi to read and write directly to a mysql database on a server.
Best,
Sam

Design pattern for Core Data default entries

I'm planning a new ios app and am not sure of what is the best way to set up a start list for a tableView on first app start. The app uses Core Data (more precisely Magical Record). Should I use some kind of (p)list (dictionary) which gets imported or should I just hard code the default entries like when the user adds something through a formula? Thank you!
There are a load of arguments for and against all different ways of doing this.
Personally I prefer just to hard code all the default entries (if there aren't thousands of them obviously).

Resources