Using CoreData outside of a TableView on the iPhone - 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.

Related

How to use Core Data without Storyboards?

I am trying to use Core Data without storyboarding in my app. I want to manage many relationships and I have a kind of singleton class to manage the access to the managedObjectContext, model, and persistentStoreCordinator I got it from this link Singleton datalayer without appdelegate
My problem is that I dont want to use StoryBoards but all the examples that I found use Storyboards. I have problems with UITableView because appDelegate doesnot found it.
I want to create a Single Application and then with a singleton to manage the context and everything, show the objects that I get in a JSON format with nsfetchedresultcontroller or with fetchrequests if I only need one item.
I am using this class. also I have another class called CoreDataTableViewController.h/m it is a subclass of UITableViewController and it has the methods of NSFetchedResultControllers. I got this of the Stanford IOS course. Then I have my MainViewController its a subclass of CoreDataTableViewController and there I get the data from the Json and put it in my ManagedObject and also I overwrite the method cellForRowAtIndexPath. But when I start the simulator I have an error. loaded the "ViewController" nib but didn't get a UITableView.
Core Data doesn't care whether you're using Storyboards or not. Just copy the Core Data related code that you need (you can create a singleton containing the relevant managedObjectModel/context, persistentStoreCoordinator objects and methods) and add it to your own project.
There are no relation between the two. If you need an example of core data that doesnot use storyboard, try Apple's. Good luck!

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.

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.

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

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.

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.