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.
Related
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 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.
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!
In an app I'm building, I'm using Core Data to cache remote content from an API for offline viewing. This all works pretty well except for one big issue: if a record on the server is deleted there's no way for me to detect that and delete its cached counterpart.
The only thing I can think of is somehow marking all the current data as 'invalid' when I pull data from the API and only mark the records returned by the API as valid again, but it seems like this is a clunky solution to the problem. Additionally, as data from the API I'm using is paginated it doesn't scale well for lots of records.
So what I want to know is: is there a better way to invalidate local cache data in response to it being deleted server-side?
I would suggest, although not the easiest route, is to have the server side cache items that are deleted and expose an endpoint you can call to get the deleted items. In a perfect world right.
What you can do is in a background thread, download all the data from the server and compare it to what you have locally. So instead of just invalidating all of it and re-parsing it back in (which can take time for large data sets), just run through and compare id's of objects on the server to your objects in CoreData. If it's there great, if not delete it from you local db. Hope this helps.
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