how is it possible to preserve data in an uitableview when the application quits? so the original information is still retained?
For that you will need some sort of persistence manager; it won't "just work" the way you have set it up now.
One way is to use Core Data - Apple has good documentation on this and their TableView template can use CoreData for data storage.
You mean you want to save the datasource of your tableview when the application exits? In that case, in your applicationWillTerminate, save your datasource to a persistent memory. Depending on the amount of data you want stored, you can decide on NSUserDefaults, CoreData, sqlite database or even a remote storage where you can send your data via a webservice.
Another vote for Core Data. It's very easy to work with and will allow you to persist your data in real time as it changes in your app.
I recommend against only persisting your data when the app exits. iOS apps can be put in the background and/or terminated at any time based on external events (home button, phone calls, notification interactions, etc) or your app could crash. If you use core data properly and save the managed object context whenever data changes your users will have a great experience.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html
Related
I have a social app, where the user can like photos... So, in order not to wait for fetching the data from the server, I want to store to the device, an array of strings, containing the photos' objectIds.. The question is, considering the user can like thousands of photos, is it good practise to use UserDefaults to achieve that?
EDIT
As pointed out by Eric Aya in the comments, NSUserDefaults aren't loaded automatically into memory when the app launches.
NSUserDefaults are loaded into memory when your application launches If you have a particularly large amount of data stored in NSUserDefaults then the time it takes your app to launch to load NSUserDefaults will be impacted by the amount of IO required to retrieve your data. The intended use case for NSUserDefaults is to store small sets of data such as default user settings.
A Plist may be a better solution (NSUserDefaults is just a Plist, the difference being is it's loaded automatically for you when the app launches). You will still have the same issues with load times when you decide to grab the Plist as you will be loading a file (the Plist) into memory. You will be able to handle this with something like a progress bar or an activity indicator which gives the user a nicer experience. than having to wait longer than usual for an app to open. CoreData is another option (usually intended for more complex data structures than strings, on the flip side it gives you the capability to scale your storage needs very easily if the complexity of your data increases), there's a fairly steep learning curve involved but it's a wonderful feature and is well documented by Apple: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/
I would also recommend storing the data on a server where you could expose it via an API and cache the response using a Plist/CoreData/Whatever you like. This way if your users change devices they will still have access to the same data as it's stored remotely.
Good luck!
i am developing an sample app where i have N number of custom button with images present in the screen.I can able to perform following operation(drag drop,rotate,change color etc.)upon these buttons.
I can also able to draw line on the screen.
So now i want to persist these data so that on restart of application i can able to see my previous objects.What is the best solution to do this?
You've got three options, all pretty simple:
store the data in user defaults
store the data in a plist in the documents directory
use core data
In all cases you'll need to transform your data into property list type format (strings, numbers, data, dictionaries, arrays). User defaults would be frowned on if you had a large amount of data. Core data could be overkill. A plist is probably your best bet on the information you have given.
You'll need to store your data either each time it changes or when the application goes into the background, and restore it if needed on launch. I say if needed, because if you are just resuming from background, everything may still be present if iOS hasn't sent you any memory warnings or closed you down.
Core Data. Use a managed object to represent each button with attributes like "color","position_x", "position_y", etc.. use sqlite as the persistent store.
We've got some data coming into our app. Sometimes it will be saved, so we've made an entity and a NSManagedObject subclass for it. Usually, though, the objects will be instantiated and never saved. I'm thinking of using another persistent store, with the NSInMemoryStoreType, as a staging area, then moving the ones we want to save into the sqlite store. Is that possible/sensible?
If it is, I'd like to clear out the staging area every so often. Is there a way to clear out just the objects assigned to the memory store?
You should read this lengthy blog post on temporary Core Data objects. It's very insightful.
http://www.cimgf.com/2011/08/08/transient-entities-and-core-data/
Can you not use the 'scratch pad' / Undo Management properties of core data?
http://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/Articles/cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-207821-TPXREF148
I have an app that connects to a website. I store its data as core data and then I use the core data to load my tableviews. I believe, I want to reload the data from the website every time I launch the app, since the user could change the data if they were to go onto the website.
What's the best way to approach this? Should I just delete the core data each time the app terminates?
It depends on how critical it is that data on the device always be current, on how long you expect users to wait before working with the app, and on what you want to happen if someone tries to use the app when the network is slow or unavailable. If you delete the store, and the app can't connect immediately, the user has no data. Even if the network is fine, the user still has to wait on network and server latency before they can start using the app. This is likely to be poor UX.
On the other hand if you allow data to persist after the app exits, the user's data might not be current, at least not at first.
If you don't ever want the data to persist after the app exits, an in-memory store might be the best choice, because it will never be saved to a file anyway.
If presenting potentially old data is OK either (a) briefly when the app starts up or (b) when the network is unavailable, a better choice would be to keep the data store but make new network calls to update the existing data. You could provide some sort of UI to indicate that updates are in progress. You might also have some in-app indication of when your app last synced with the server.
I'm not sure of how your application makes use of the Core Data stack, but if you don't care about (or don't have) changes made locally on the device, I'd say the easiest way to refresh the data from the server is just what you said: wipe out the store file, re-create it and import the data from the server. However, I wouldn't do this systematically on each application launch. Make sure the user knows about it and think of providing a "refresh" button to trigger that procedure. Also make sure you can download new content from the server, before wiping out the local store.
If you want to load all the data in memory (and also want to do updates) then you can create a managed object context backed by a NSAtomicStore persistent store subclass. In the NSAtomicStore subclass you can implement the read: (load: method) and the CRUD actions (newReferenceObjectForManagedObject:, save: and updateCacheNode:fromManagedObject: methods)
I have a iPhone app which loads json from my webservice.
I think its a good idea to store the data with Core Data but how should I sync the data with my webservice?
For example, say that I show a UITableView with the information stored in Core Data, and then I would like to update the object.
Are there any design patterns or ideas for this?
Each time your app loads it should try to load info from the web service. If it can't connect, use the local data. You should timestamp your data on the server and in the app so that you can track when the information was last updated. You can also offer a button to manually refresh the data. (Like the Mail app.)
EDIT:
To make asynchronous requests, you can see the ASIHTTPRequest library.