Every time I run an app I have to download new data - ios

I'm developing an iPhone app with latest SDK and XCode 4.5.2.
This question may be valid for any mobile platform.
I need to connect to a web service when app stars and download some information. This information could change on server: some registers could be deleted, updated or inserted.
I problem is that I don't know where to store that information. I think it's a bad idea to store it on device memory. I think it's better to store on a text file or a database. What do you think?
I have another question is: How can I know if some data has been changed on server? I think it's a bad idea to download the same data every time user stars app.
If I want to do this, connect to web service at star up, when splash screen is shown. Where I have to put the code, on AppDelegate?
Any suggestion are welcome.

Where I have to put the code, on AppDelegate?
Having the code in AppDelegate is not a very good idea. Instead you can have a viewController as splash screen. Set this viewController as your rootViewController. Do your initial web service call here and once you are done with it move to the first screen of your application.
How can I know if some data has been changed on server?
You will have to communicate to the server your version of the data. So set a version number for the data at the server side and keep track of it while downloading data. So if you pass the version number to server when you call the web service, the server can decide if there is any new data available. This way you can avoid downloading data that you have already downloaded.
where to store that information?
It depends on the data size that you want to store. And its security.
I think using database like Sqlite3 is a good option.
Hope this helps.

Where I have to put the code, on AppDelegate?
It depends on the behavior of your app as to where you decide to download the data. Typically if your app is supposed to display the latest data every time it goes to a new view then the data should be downloaded when that view is accessed. That way you will always be showing the latest available data to the user.
You would typically avoid putting too much into application: didFinishLaunchingWithOptions:, especially network connections as you dont know what kind of connection the user may have. If the connection takes an extended amount of time and application: didFinishLaunchingWithOptions: takes too long to return iOS will kill your application for taking too long to open which isn't good for the user experience at all.
How can I know if some data has been changed on server?
You will want to be passing either a last-modified timestamp or etag in the header of the response from the server. When you get the header response from the server you can than check against the previous last-modified data that you recieved or if the etag is different you know that the data has been changed. Doing it this way is a lot quicker than downloading all of the body to check a single parameter, if that parameter hasn't changed then you have just downloaded a bunch of data for nothing. I would discourage against versioning, versioning is more used for API changes rather than checking if data is out of date or not.
Storage Location?
It depends on the quantity of data you are going to be downloading, but I would advise using core data typically. The problem with storing data into a file and then saving that file is that every time you open the app you will have to break down that data into something useable before you can search through it etc. With core data you will have fully formed objects that you can search against using NSPredicate making everything a lot simpler to use in the long run.
If your just downloading a few settings then you could get away with something like a file or NSUserDefaults but if you are going to be displaying a list or collection of data to the user it's best to use core data.

How can I know if some data has been changed on server?
Use a variable version in your application and server also. Send application variable version with API and compere both version before downloading the data.
Where I have to put the code, on AppDelegate?
either can use applicationDidBecomeActive or didFinishLaunchingWithOptions depend on your requirement
- (void)applicationDidBecomeActive:(UIApplication *)application
OR
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Where to store that information
depending on the size of data and your requirement you can decide where to save the data
You can save data in UserDefault
You can save data as File in NSTemporaryDirectory / NSDocumentDirectory / NSLibraryDirectory. I think NSDocumentDirectory is fine for you.
You can save data in sqlite or CoreData

Related

How to handle data locally that is not neccessory to download from sever every time in ios?

I am going to display list of cities and countries. I have to get it from server using web service. but city and country are not changed every time. so we don't need to call that web service every time. so we can store all the information locally. what is the best way to handle this situation?
Yes you are right we don't need to call web service every time. You can use coredata in this situation. Using coredata you can manage all data locally and retrive back from coredata.
Core Data is the best one to use for non-trivial data storage. It can reduce the memory overhead of your app, increase responsiveness, and save you from writing a lot of boilerplate code.
Refer this link : http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
You can do this in several ways
you can use nsurl session caching
store data in local db (coredata or sqlite) http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
For simple data you should use NSUserDefaults
use plist file http://www.theappcodeblog.com/?tag=ios-saving-data-to-plist-tutorial
http://code.tutsplus.com/tutorials/ios-sdk-working-with-nsuserdefaults--mobile-6039
please check the following also
storing data locally on the iphone
Store data locally IOS
There is some more option to locally
1. Using LocalDB - its little bit pain but not bad that much
2. Using XML file - you should have the method to retrieve the data.Its pretty easy
3. Using Plist - This is also very easy can done through few lines.
4. NSUserDefaults - This can be use only as few cases like User information, store tokens.
Got it!

App structure iOS and Realm: create database when app is installed

I am very new to iOS. I am developing an app with data persistence. I have decided to use Realm for that purpose.
I must to create the database and load data the first time that app runs. I get data from a Web Service in JSON format. I will implement some strategy to update this database later, maybe with iOS Silent Push notifications.
I have read and I have worked about Realm, loading data from JSON... to learn about that.
Now, I need to apply this in my project but I don't know how to start. I need some clues about general idea for the app:
How can I organize my app to load data when it is installed? At what point should I create the database and load data?
I have thought to create a global Realm object y AppDelegate and use it as a global variable. Is it a good idea?
Do I need to set a path for my database? Can I user default path?
If you are looking for a place to start, you can check out the example apps of this UI component add-on for Realm: ABFRealmGridController.
The controller is a subclass of UICollectionView and the example app should demonstrate most of the functionality you are curious about. The example uses the controller to display the top news stories from the New York Times. This involves making a request to their API and loading the JSON response data into Realm.
When to load the data is dependent on how you want the app to function. If the data will be the same for each user, you could bundle the Realm file with the app pre-populated with data.
The ABFRealmGridController example loads data when the user clicks the refresh button and performs the JSON handling on a background thread; a general best-practice.
Finally, unless you have multiple Realms or need to store the file in a specific path, it is probably simplest to use the default path.

Best way to check for data updates on webserver

I have an application which uses the data from web server. When you first launch the app, it downloads the data and then work with it. But what if the data on web site was changed. How can I know from the application that the data was changed, and if so, what data should I download?
My first idea was each time when you run the application to check the number of entries in the local database on your phone and the number of entries on web server, and if they are not equal, delete all data in local database and then download all data again. But I think that it will take more time than if the application just loads 5-10 needed records instead of all data.
The second idea was when the information on the site changes, website somehow has to inform the application to load some records. But I don’t know if it is possible to do(
Another idea was to compare the id of the last entry in the application database with last id on website. And if they are not equal download the information from the next id.
Are there any suggestions how can I accomplish this?
I am not sure that you have any database or web services but my suggestion is parsing data from the web with JSON or XML.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/
this class reference is will be clear for you.
Also in my opinion, if you are new in swift and want to choose easy way for this operation search for iOS package managers.
If you want to use a package manager for your project, e.g Pod
https://cocoapods.org/pods/Alamofire
would be a good startig point.
Alamofire is an HTTP networking library written in Swift.
Hope to helped you

Using JSON without Core Data

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.

Updating Sqlite from web server?

I am currently building an iPhone app that is using Core Data and sqlite databases where the user will be reading static information from the database throughout the app. I have the issue where we may update the information in the database but not want to do a full update of the app, just the database. Can someone please help me out with either a easy function or a tutorial of how to go to a website or server and download the file which will replace the database that we have already put into the app? I'm new in xcode and I`m doing my first app.... thanks for your help
I think what would be a good idea is for your website to publish the data that must be stored in sqllite over REST, possibly in JSON or XML format.
This blog post describes how you could do just that. I must say that its approach to retrieving the content from the webservice is kind of low-level but it'll get the job done. Maybe RestKit can help you take care of all the low-level networking/http stuff.
I assume you want the static data locally so you don't require a constant internet connection for your app to work. Another option is to request the static data from the web and persist it in a file (NSUserDefaults etc...). But, that depends on how complicated the static data is and whether you have to query into that data. If you need to issue queries on that static data, a DB is definitely better.
You can also do a combination where you download updated DB if available async while your app works. You could have a setting in user defaults which is the current static data DB. If updated, you switch the current setting and re-establish the DB connection under a lock.
Here's how to make an http request using iOS.
rest web services in iphone
If you're downloading db data, don't convert the NSData to a string like in that sample ...
Also, ASI-HTTP-Request is popular. Here's samples on how to download a file:
http://allseeing-i.com/ASIHTTPRequest/How-to-use
http://www.cocoadev.com/index.pl?NSUserDefaults

Resources