iPhone - pull data directly from a database to tableview cells? - iphone

I currently have an application with a tableview, I've followed the basic tutorials and use an NSArray and NSDictionary that I populate myself and use this to populate the cells in the tableview.
I have also been able to create a test database and I can pull the information from here into an array and again add this array to an NSDictionary and populate the data in the cells of the tableview.
However it doesn't make sense to me that I have to copy the data from my database into memory (array) and then use this to populate the cells, is there a way to populate the cells of a tableview without needing to use an Array and dictionary and instead access the info for each cell directly from the database?
Or is pulling data from the database into memory and using it from there the recommended way?

Take a look at the Core Data Programming Guide. Core Data uses so-called "faults", that aren't fired until you access them. Additionally, Apple provides a NSFetchedResultsController to manage a UITableView with a Core Data-based database.

Related

How to save data from different view controllers and load it in another view controllers in iOS

I'm new to iOS. After going through a lot of and documents i'm confused .This is what I have to do.
I have several view controllers each has NSString values which I'll enter in a textfield and save it in a common place and when I need to view the data, it would be displayed in aUITableview. I know how to create a UITableview and load the data in it. But I have to know how to save and load that.
So far I have tried somethings . If I enter a new field, the old contents are overwritten.I don't know where to start..Can anyone give me step by step logic(not code). So that I can follow that.
I tried using NSdefaults but its not efficient as i expected
You can save the data in multiple ways
Use CoreData to save the data. You will find some good tuts on how to use CoreData
Use an SQLite database without CoreData
Save data into your app delegate or a view controller accessible from the final view controller
Pass the data from all viewcontrollers to the final viewcontroller
Save data in a plist
The possibilities are endless. What works best for your project is what you should use.
1-you can save common data with your app delegate interface . then you can access it from others interfaces
2-you can use NSUserDefault to store data with keys
3-create NSString object and with passing from view to another one pass data to new NSString object
There are two options available storing and retrieving data in different view controllers.
1)NSUserDefaults is best option for storing data and accessing in any other view controllers.
The NSUserDefaults class provides convenience methods for accessing common types such as float, double, integer, Boolean. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.
This is very easy and best method for storing and retrieving data.
if you want to read about NSUserDefaults, here i am sharing document.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
2) You would create properties when you want them to be accessible outside the class or other view controllers.
Create property in this way. #property (nonatomic, retain) NSArray *arrayData; and then you can use this array value in other view controllers also.
Properties replace the accessor methods for objects.
using a db is better choice here. But if you want those data only dynamically then u can save it in a array and get those common data by delegates.

Refreshing CoreData Table?

I can refresh any non-CoreData loaded tableView by calling [nameoftableView reloadData];
But when I call this on my CoreData loaded TVC it does not reload the table. Does this work differently?
It does update it contents if I stop and start the app again - so that it reloads the database, so I know it has the updated content in it.
Cheers Jeff
Without details is difficult to know what is going on, but if you use a simple NSFetchRequest I think you need to execute the query and call reloadData again.
A simple note
I suggest you to use NSFetchedResultsController when dealing with Core Data and UITableViews. raywenderlich has a tutorial on how to use that class in Core Data.
First of all it allows you to deal with a lot of data displayed in a UITableView. In particular, if for the NSFetchRequest you use with, you set a batch size, data will be retrieved in "batches". For example, the first 10. Then if you scroll the other 10 and so on...
In addition you can deal with data changes (update, insertion or deletion) for free using NSFetchedResultsControllerDelegate class (Reference NSFetchedResultsControllerDelegate).
Hope it helps.

How to store information in a detailed view from a TableView

I have a bunch of information from a few views (stored in a singleton) that I collected from the user, now all displayed in one confirmation view. How do I take this information and create a new UITableViewCell that one can click on to see the entered information.
And, although this comes later, how do I make sure this data is stored between sessions and never deleted unless done so by the user.
Thanks for the help.
Showing the data in a UITableView is quite trivial, and I suggest you check out the UITableView Class Reference, UITableViewDelegate and UITableViewDataSource protocols to find out how you can do that.
For the storage of the data, one of the ways to do so is by using the NSUserDefaults.
Once you have stored the user's data, you can access the NSUserDefaults from a different view without having to pass any objects between those views / controllers.
Generally you'll want to use properties and delegates to pass data between views.
As far as persisting data, for light-weight data (like preferences) use NSUserDefaults. For more complex data, use Core Data.

Using an NSMutableArray instead of an NSFetchedResultsController

I've recently come to learn that NSFetchedResultsController is an extremely buggy class and its been causing me headaches for a while now with my rather large Core Data app.
Would it be appropriate to use an NSMutableArray to feed the table view instead of an NSFetchedResultsController? What I'm talking about is, temporarily creating a fetched results controller to grab the objects from my managed object context, creating a new NSMutableArray with the fetchedObjects from the fetched results controller, then using that to feed my table view.
Are there any benefits to using NSFetchedResultsController directly over an NSMutableArray to supply data to my table view?
You don't have to use a fetched results controller. It is a new convenience class and you can always do things old school.
I presume that because you want to use mutable array that you intend to use add and remove objects. This is not a problem except you have to everything manually. If something else modifies the data, you have to register notifications to monitor the changes and refetch as needed.

Using CoreData outside of a TableView on the iPhone

My application has a simple data model with 70 read-only records grouped into six categories. Currently the persistent data is stored in an XML file which I parse on application launch, and create objects for each record which are stored in an NSMutableArray.
I then populate a UIPickerView from the objects stored in the array, and when the user selects a row in the picker, display content pulled from the same objects.
Using CoreData and SQLite would use much less code, but it seems to be designed to work with UITableViews exclusively.
Has anyone used CoreData to fetch records outside of the UITableView interface?
Thanks
jk
Core Data is not designed to work with UITableViews only. Although it's really easy to Core Data with the NSFetchedResultsController to populate a table view you can still do all the fetches you want on your an.
Just make your own NSFetchRequests to get the data you want. From the list of objects, particular objects or just single values you can use this date for whatever purpose intended.
I put my custom Core Data methods into the application delegate, where they can be called from any class. To do this, I add a #define as follows:
#define UIAppDelegate ((MyAppDelegate *) [[UIApplication sharedApplication] delegate])
I can then call my methods like so:
[UIAppDelegate fetchBooks:managedObjectContext];
A UITableView is not required for any of this.