Introduction to Core Data: Your First Step to Persistent Data
Editor’s note: After we published the tutorial about saving data in plist file, some readers asked about Core Data and how we can use it to save persistent information. This week, we work with Ziad Tamim, an independent iOS developer, to give you an introduction of Core Data and work with you to build a sample app using Core Data.
This tutorial talks about persistence on iPhone (or other iOS devices). What I mean by persistence is to make data that’s in your apps stay around between application launches. Persistence lets users store persistent data and also retrieve it, so that users don’t have to reenter all their data each time they use their applications. There are multiple ways to store data in iOS devices but most of them aren’t good enough to store a complicated data. They are usually used to save settings or to preload some data such as “Property List” and “Archiving Objects”. So that’s why we’ll go through Core Data to see how you can utilize it to manage data in database.
The focus of the tutorial is to provide a practical introduction of Core Data framework. I expect you’ve already gone through our tutorials about Storyboard and UITableView. I will not give in-depth explanation about how to create view controller in Storyboard but you can always refer to the earlier tutorials to gain better understanding.
Core Data is not a Database
When we talk about persistent data, people probably think of database. If you are familiar with Oracle or MySQL, you know that relational database stores data in the form of table, row and column, and it usually facilitates access through what-so-called SQL query. However, don’t mix up Core Data with database. Though SQLite database is the default persistent store for Core Data on iPhone, Core Data is not a relational database. It is actually a framework that lets developers store (or retrieve) data in database in an object-oriented way. With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL.
To illustrate the concept, let’s begin and create your first app using Core Data. This app is called My Store. It is a very simple app that stores all devices you have by collecting the name, version, company.
Creating a Sample App with Core Data
First let’s create a project with Core Data. Open Xcode and create a new Project, choose the template Empty Application as shown below.
At the next screen, enter MyStore as a name of the project, select iPhone in Devices family and don’t forget to select the options Use Storyboards, Use Core Data, Use Automatic Reference Counting. Press next and create.
Core Data Stack
Before we start working on the project, you first have to understand the Core Data Stack:
Managed Object Model – It describes the schema that you use in the app. If you have a database background, think of this as the database schema. However, the schema is represented by a collection of objects (also known as entities). In Xcode, the Managed Object Model is defined in a file with the extension .xcdatamodeld. You can use the visual editor to define the entities and their attributes, as well as, relationships.
Persistent Store Coordinator – SQLite is the default persistent store in iOS. However, Core Data allows developers to setup multiple stores containing different entities. The Persistent Store Coordinator is the party responsible to manage different persistent object stores and save the objects to the stores. Forget about it you don’t understand what it is. You’ll not interact with Persistent Store Coordinator directly when using Core Data.
Managed Object Context – Think of it as a “scratch pad” containing objects that interacts with data in persistent store. Its job is to manage objects created and returned using Core Data. Among the components in the Core Data Stack, the Managed Object Context is the one you’ll work with for most of the time. In general, whenever you need to fetch and save objects in persistent store, the context is the first component you’ll talk to.
The below illustration can probably give you a better idea about the Core Data Stack:
Defining Managed Object Model
Let’s move on to build the app. The first step is to open the Data Model named MyStore.xcdatamodeld and define the object model. Here we’ll define a Device entity that will be used to store the device information to database. To create an entity, click the + button in the bottom-left of the editor view and name the entity as Device.
Once you create a new entity, you need to add attributes to it. Click on the + button in the attributes section to do that. Add three attributes including name, version and company. Set the type as String.
Designing the User Interface
The next thing we need to do is to create the Storyboard that defines the views of our app. Navigate to File > New > New File and choose Storyboard in the User Interface template. Click next and select the iPhone device family, click create.
Once created, make sure to set the “Storyboard” you’ve just created as the main storyboard in the project setting.
Also don’t forget to delete all the generated code in the method -(BOOL)application:application didFinishLaunchingWithOptions:launchOptions inside the AppDelegate file. The method should be as simple as this:
1 2 3 4 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; } |
Go to Storyboard and create the user interface like below:
First, drag a Table View Controller and embed it in a Navigation Controller. Drag a button to the top-right part of navigation bar and set the identifier as “Add”. This will automatically change the button to a “+” button. Next, select the prototype cell and change its style to “Right Detail”.
Drag a View Controller to the Storyboard and add a Navigation Bar to the top of the screen. Next, drag two buttons into the navigation bar. Name one as “Cancel” and the other one as “Save”. In the content view, add three text fields and name the placeholder attributes as “Name”, “Version” and “Company”.
This detail view will be shown when user taps the “+” button in the table view controller. So finally, press and hold the Control key, click the “+” button and drag towards the detail view controller. Select “Modal” as the Segue action to connect the table view controller and detail view controller.
Creating View Controller Classes
Create a new class by right-clicking on the MyStore folder > New File > Objective-C class, and name the class as DeviceViewController. Make it as a subclass of UITableViewController. Navigate to the Storyboard, select the Table View Controller and associate it with the DeviceViewController class.
Once done, do the same steps to create a new class named DeviceDetailViewControllerUIViewController. Again, go to Storyboard and set the custom class of the detail view controller as the “DeviceDetailViewController”.
Lastly, wire up the UITextFields to the DeviceDetailViewController header file and create two action methods for the save and cancel buttons respectively.
Your code should like this:
1 2 3 4 5 6 |
@property (weak, nonatomic) IBOutlet UITextField *nameTextField; @property (weak, nonatomic) IBOutlet UITextField *versionTextField; @property (weak, nonatomic) IBOutlet UITextField *companyTextField; - (IBAction)cancel:(id)sender; - (IBAction)save:(id)sender; |
Diving into Core Data
With the user interface, it’s time to go into the details of Core Data. Apparently, there are a couple of areas we have to implement:
- Save device information in the Detail View Controller
- Fetch device information from persistent store (i.e. SQLite database) and populate the data into Table View Controller
We’ll look into the implementation one by one.
Saving Device Information
First, we need to implement the DeviceDetailViewController to let user add the devices to the database. Open up the DeviceDetailViewController.m file and add the following code after @implementation DeviceDetailViewController:
1 2 3 4 5 6 7 8 |
- (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context; } |
Recalled that we’ve selected the Core Data option when creating the project, Xcode automatically defines a managed object context in AppDelegate. This method allows us to retrieve the managed object context from the AppDelegate. Later we’ll use the context to save the device data.
Next, we’ll implement the “save” and “cancel”, add the necessary code to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
- (IBAction)cancel:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)save:(id)sender { NSManagedObjectContext *context = [self managedObjectContext]; // Create a new managed object NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context]; [newDevice setValue:self.nameTextField.text forKey:@"name"]; [newDevice setValue:self.versionTextField.text forKey:@"version"]; [newDevice setValue:self.companyTextField.text forKey:@"company"]; NSError *error = nil; // Save the object to persistent store if (![context save:&error]) { NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); } [self dismissViewControllerAnimated:YES completion:nil]; } |
When user taps the “Cancel” button, we expect the app to close the detail view controller. Line 2 of the above code invokes the dismissViewControllerAnimated method to dismiss the current view controller with animation.
For the “save” method, we first grab the managed object context. Every object that Core Data stores is inherited from NSManagedObject. So we first create a new instance of NSManagedObject for the “Device” entity that we’ve defined in the object model. NSEntityDescription class provides a method named “insertNewObjectForEntityForName” for developer to create a managed object. Once you created the managed object (i.e. newDevice), you can set the attributes (name, version, company) using the user input. Lastly, we call up the “save:” method of the context to save the object into database.
You can now hit the Run button to try out your app. Tap the “+” button to bring up the Detail View and save a new device. However, the new device is not yet displayed in the table. Let’s move on to see how you can fetch the device information from database.
Fetching Device Information
Open DeviceViewController.m, add a “devices” property to it so we can save all the devices received.
1 2 3 4 |
@interface DeviceViewController () @property (strong) NSMutableArray *devices; @end |
Again, add the following code after “@implementation DeviceViewController” for grabbing the managed object context:
1 2 3 4 5 6 7 8 9 |
- (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context; } |
Next, add a viewDidAppear method:
1 2 3 4 5 6 7 8 9 10 11 |
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Fetch the devices from persistent data store NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"]; self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; [self.tableView reloadData]; } |
Like what we’ve done in the Detail View Controller, we first grab the managed object context. To fetch device information from database, the code above creates a new instance of NSFetchRequest and set the entity Device and invokes “executeFetchRequest” method to retrieve all the devices from the database. If you are familiar with relational databases, this instance works like the SELECT clause.
Populating Device Information into Table View
As we would like to display these data into the table view we need to implement the data source of it, to do that add the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return self.devices.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]]]; [cell.detailTextLabel setText:[device valueForKey:@"company"]]; return cell; } |
If you have used UITableViewController before, the code above is the simple way to display data into the table view. If you check the code you will notice the NSMangedObject is pretty much like NSDictionary. It gathers all the attributes of the entity (i.e. Device) and you can simply use the “valueForKey” method to grab the attribute value.
That’s it. Let’s try to run the app and test it. If everything is okay, your app should like this. Try to add some devices and the device information should be populated automatically in the table view.
What’s Coming Next
It’s a lengthy tutorial but we try to elaborate the implementation as detail as possible so everyone can follow. As you can see, with Core Data, you don’t have to care about SQL to save and retrieve data from database. Everything is done behind the scene. This tutorial kicks off the first part of Core Data series. Later we’ll talk more about object relationship.
Lastly, let me end the tutorial with an exercise. Try to complete the app by adding the functions that let user update and delete an existing device by selecting a row in the table view.
Hope you enjoy the tutorial and feel to leave us comment.
Update: Check out part 2 of the Core Data tutorial series!
Comments
Curlypaws
AuthorThis is really good – but in trying to add update and delete capabilities I’m struggling to find out how I’d identify the correct record and then update/delete it. After all, there is nothing that makes sure the entries are unique. Should I be looking at NSFetchedResultsController?
Simon Ng
AuthorYou can check out the new tutorial:
http://104.131.120.244/core-data-tutorial-update-delete/
Paranoid
AuthorIt’s a great core data tutorial, thx.
Bob
AuthorThanks for the article on Core Data. It is really good. Here is an open source app that is a must have when working with core data although it could use an easier way to interact with the on device Core Data files.
https://github.com/yepher/CoreDataUtility
B
Author#import
#import “AppDelegate.h”
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
} –Got a Thread 1:signal SIGABRT – program crashed after i tried to hit save. I have another core data tutorial with the same error new to programming….please any advice??
Richard
AuthorI’m getting the same error too. Can’t find what I seem to be doing wrong.
Juan Nozaleda
AuthorChange the Cell Style into left detail
Priyal Jain
AuthorIt isn’t working
Nathaniel Lee
AuthorCan you please post what it says in the console?
Jason
AuthorGetting the same error as well. Felt like I learned quite a bit but stuck with this runtime error.
Jas
AuthorIf i chose a tabbed application and manually added the core framework, will the following code still work to obtain the managed context?
@implementation DeviceViewController
– (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
Visitor
AuthorIn, DeviceViewController.m, method:
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathNo visible @interface for ‘UITableView’ declares the selector
‘dequeueReusableCellWithIdentifier:forIndexPath:’
Why it is giving me an ARC issue?
Chavira Romero
AuthorThe problem that you’re having is that you didn’t set the identifier of the Table View Cell to “Cell”, is not in the tutorial but it is in the screenshot: http://104.131.120.244/wp-content/uploads/2012/12/MyStore-App-Table-View-Controller.jpg
Greetings!
Tien Dung.
AuthorThat’s right, it’s maybe a missing.
P/S: Add this into tutorial bro !!!
Patrick Molloy
AuthorAha! Thats the issue that Ihad too. Thanks for this 🙂
Marco Rosas
AuthorYeah, the same problem I faced… thanks for this!
Alexandru
Authormaybe you try to call the function on the wrong object?
Pakap84
AuthorGreat tutorial but I’ve a big problem. At the end, when I want to run my application (i’ve a dark screen…). Nothing is display and there are no errors in Xcode…
I don’t understand why…
Thx
VvV
AuthorI face the same issue. Did you figure out the problem.
Any help appreciated.
Thanks.
RLG02
AuthorSee comment above about this issue– you may need to add the storyboard to your project. Or you may also need to comment out (or remove) the window creation and display code in AppDelegate.m. This is explained in the original instructions above.
jagveer Rana
Authori have same problem ,, any body can help me??
Pakap84
AuthorGreat tutorial but, when I use the Iphone simulator, i have a dark screen (nothing displayed). For xcode, there is no errors…
iamxyt
AuthorSetting “Main storyboard file base name” information in the MyStore-info.plist file, which tells the Xcode which storyboard you are using,
scud
AuthorI’m getting a SIGABRT error when I try to build at:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
in the DeviceViewController.m file. Can anyone help?
Juan Nozaleda
AuthorChange the cell style into left detail
Priyal Jain
AuthorWhats the difference between left/right Detail ?
jadacoast
AuthorHi guys, I’m getting the following issue however no errors are reported in the code and it says ‘Build Succeeded’ after I go to run it. Could anyone please help?
{@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([myStoreAppDelegate class
]));
}
}
Nathaniel Lee
AuthorYou still there?
wahiba
Authoryou have to delete a Breakpoint in your code
ish3lan
Authorin DeviceDetailViewController:
add .text after calling uitextfields :
[newDevice setValue:self.nameTextField.text forKey:@”name”];
[newDevice setValue:self.versionTextField.text forKey:@”version”];
[newDevice setValue:self.companyTextField.text forKey:@”company”];
as we should give a NSString to be saved not a UITextField
Hope that will help
🙂
steven
AuthorSuch a great tutorial! But the problem I am struggling with is when I try to add a fourth text field for a device, there is bugs. Is there some I need to notice when I add text fields?
Radoslav
AuthorThis tutorial was very helpful.Thanks.
Just a question: Is it possible create more complicated queries e.g take devices with version more than 5.1, and how?
k0e3
AuthorOnce done, do the same steps to create a new class named DeviceDetailViewControllerUIViewController. Again, go to Storyboard and set the custom class of the detail view controller as the “DeviceDetailViewController”.
This part is a little confusing. You mean a new class named DeviceDetailViewController that’s a subclass of UIViewController right?
Cristiano72
Authori have this problem
aradddd
AuthorI have the same problem, please help us
Robert
Authormistake?
DeviceDetailViewControllerUIViewController. Again, go to Storyboard and set the custom class of the detail view controller as the “DeviceDetailViewController”.
should read?
DeviceDetailViewController. Make it as a subclass of UITableViewController. Again, go to Storyboard and set the custom class of the detail view controller as the “DeviceDetailViewController”.
Darkmouse
AuthorThank you for your tutorials and for your work, they are very usefull.
Thank you very much¡¡
Krishna
AuthorNice tutorial. Got it at the first time. Thanks for clearing the fact that the core data is a different that other Databases like MySQL or SQL server. I was confused with it all the time.
Ashish Porecha
AuthorUnder Persistent Store Coordinator is a line:
Forget about it you don’t understand what it is.
WTF?!?! HAHAHAHAHAHA!!!
Lovevapour
AuthorGreat tutorial!! thks! 😀
John Kealy
AuthorGreat tutorials! For some reason though when I click “Add” it does not fire up the modal… Any ideas?
Priyal Jain
AuthorSame issue :/
jacob banks
AuthorWhen i run it it is saying Application windows are expected to have a root view controller at the end of launch. and it is not showing my tableview just a white screen
jacob banks
Authorcan anyone help with that?
RaviKumar Yaganti
Authoryou have to remove the default code in the didfinishlaunchingwithoption in appdelegate.m
kkk
Authorplease help me. i have attributes winnumber Set the type as Integer32. Where I corrected in this project.? thank you.
Canyon Grand
Authorcan you help me ? Add Searchbar in tableview with Coredata .
Guest
AuthorYour Source code of the CoreData is not working in my devices!!!
sankar ram
AuthorPlease help me in this problem!! I am getting error in this line in DeviceViewController.m file
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Juan Nozaleda
AuthorTry to change the cell style. put left detail
nuclear
AuthorThis tutorial may have some issue, it is not working for most people from the comments
planbmn
AuthorSame issue as jadacoast. Applied other suggestions but still getting NSException. Should this tutorial still work for iOS SDK 7.0?
pradeep
AuthorCould somebody help me ? I’m getting the below error after hitting ‘save’ button. I’m using xcode 5
2014-02-26 00:00:27.061 MyStore[1916:70b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unacceptable type of value for attribute: property = “name”; desired type = NSString; given type = UITextField; value = <UITextField: 0x8b53b20; frame = (20 88; 280 30); text = 'abc'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = ; layer = >.’
*** First throw call stack:
(
0 CoreFoundation 0x01a9f5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018228b6 objc_exception_throw + 44
2 CoreData 0x0029631e _PFManagedObject_coerceValueForKeyWithDescription + 3614
3 CoreData 0x00263c49 _sharedIMPL_setvfk_core + 185
4 CoreData 0x002954a6 -[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) _setGenericValue:forKey:withIndex:flags:] + 54
5 CoreData 0x00283b99 _PF_Handler_Public_SetProperty + 105
6 CoreData 0x00283b23 -[NSManagedObject setValue:forKey:] + 163
7 MyStore 0x000040e2 -[MSDeviceDetailViewController save:] + 226
8 libobjc.A.dylib 0x01834874 -[NSObject performSelector:withObject:withObject:] + 77
9 UIKit 0x005920c2 -[UIApplication sendAction:to:from:forEvent:] + 108
10 UIKit 0x00866c9b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
11 libobjc.A.dylib 0x01834874 -[NSObject performSelector:withObject:withObject:] + 77
12 UIKit 0x005920c2 -[UIApplication sendAction:to:from:forEvent:] + 108
13 UIKit 0x0059204e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
14 UIKit 0x0068a0c1 -[UIControl sendAction:to:forEvent:] + 66
15 UIKit 0x0068a484 -[UIControl _sendActionsForEvents:withEvent:] + 577
16 UIKit 0x00689733 -[UIControl touchesEnded:withEvent:] + 641
17 UIKit 0x005cf51d -[UIWindow _sendTouchesForEvent:] + 852
18 UIKit 0x005d0184 -[UIWindow sendEvent:] + 1232
19 UIKit 0x005a3e86 -[UIApplication sendEvent:] + 242
20 UIKit 0x0058e18f _UIApplicationHandleEventQueue + 11421
21 CoreFoundation 0x01a2883f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
22 CoreFoundation 0x01a281cb __CFRunLoopDoSources0 + 235
23 CoreFoundation 0x01a4529e __CFRunLoopRun + 910
24 CoreFoundation 0x01a44ac3 CFRunLoopRunSpecific + 467
25 CoreFoundation 0x01a448db CFRunLoopRunInMode + 123
26 GraphicsServices 0x039009e2 GSEventRunModal + 192
27 GraphicsServices 0x03900809 GSEventRun + 104
28 UIKit 0x00590d3b UIApplicationMain + 1225
29 MyStore 0x000045ad main + 141
30 libdyld.dylib 0x020dd70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
pradeep
AuthorIt’s my mistake. I found what i had done wrong. I forgot to add “.text” in [newDevice setValue:self.nameTextField.text forKey:@”name”]; It is solved now after adding “.text”. Very nice and easy tutorial to start with core data. Thanks for the tutorial
Marco Boldetti
AuthorHi guys, i have a question? like implementing core data in a existing project?
Mariyan Tashev
AuthorThis
http://craig24.com/2013/12/how-to-add-core-data-to-an-existing-project-in-xcode/
Rob
AuthorIf you’re running into a runtime exception when you click the + (add) button, you probably didn’t set the subclass for DeviceDetailViewController properly. It should be UIViewController not UITableViewController. The instructions above do not make this clear.
dodets
Authori have following error after click ‘+’ button :
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UITableViewController loadView] loaded the “nXf-mE-RFU-view-uFh-s5-Woc” nib but didn’t get a UITableView.
what should i do? anyone can help?
thanks
kamneed
AuthorSee my answer to sajinseethi
Priyal Jain
AuthorIt’s really handy for other if you could repost your solution because searching for sajinseethiis really a cumbersome task.
kamneed
AuthorDelete three not needed methods in DeviceDetailViewController.m:
– (id)initWithStyle:(UITableViewStyle)style
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
and
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and see Rob’s answer regarding UIViewController not UITableViewController.
Priyal Jain
AuthorThanks. 🙂
sajinseethi
AuthorI’m getting this error !! Whats this about ? Thanks…
2014-05-29 18:12:48.015 MyStore[7227:60b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UITableViewController loadView] loaded the “uwL-Dg-77k-view-cRO-bK-ZrR” nib but didn’t get a UITableView.’
*** First throw call stack:
kamneed
AuthorDelete three not needed methods in DeviceDetailViewController.m:
– (id)initWithStyle:(UITableViewStyle)style
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
and
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and see Rob’s answer regarding UIViewController not UITableViewController.
Kiran
AuthorNice tutorial
Mustafa Kuru
AuthorIt works, thanks
Czarek
AuthorAdd button does not trigger seque, anyone know how to fix that?
Priyal Jain
AuthorDid you figure it out?
www.imtikon.com
Authorthis is a good one as a tutorial
RaviKumar Yaganti
Authorview is not dismissing…….
– (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
Raghunath
AuthorI am not able to create NSManagedObject instance (*newDevices).It doesn’t allow me to write NSManagedObject.In Screenshot ,Just i have Copied.It allows only NSManagedObjectContext and NSManagedObjectModel…. Whats the Problem??
Raghunath
Authorprovide me solution…….
Guest
AuthorTry importing CoreData at the top.
#import
Napster
AuthorHi The DeviceDetail Page crashes as soon as I enter the input in textboxes. Any help will be much appreciated?
kuldeep tanwar
Authorhow can you miss
#import
i’ve benn banging my head around for weeks 😀
kuldeep tanwar
Authorhow can you miss
@import CoreData;
Hishou
AuthorWould it be possible to update the Core Data series of tutorials for Swift 3? thanks!
joanpozo
Authormy friend was searching for LITHO TOPS Form 3285 earlier this week and encountered a website with lots of form templates . If you are looking for LITHO TOPS Form 3285 too , here’s a
https://goo.gl/xEVYnU
.Alexandru
AuthorI already worked with CoreData before but some time passed since then. And it looks like some things changed since then.
I included CoreData from the beginning, but I couldn’t access the ManagedObjectContext from AppDelegate, like it is explained inside the tutorial. So this didn’t work anymore. Looks like Apple changed something since than, and moved the ManagedObjectContext to the PersistentContainer.
– (NSManagedObjectContext *)managedObjecteContext{
NSManagedObjectContext *context = nil;
id delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if( [delegate performSelector:@selector(persistentContainer)] ){
context = [[delegate persistentContainer] viewContext];
}
return context;
}
Like in Swift, try to use the (AppDelegate *) class specification. The swift pendant is the same
let context = (UIApplication.shared().delegate as! AppDelegate
Genie Music
AuthorYou saved my life, it works like a charm 😀
Alexandru
AuthorGlad to hear it helped!
It took me some hours to find the problem xD
Kashyap Bhatt
AuthorHello,
I am getting this error:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name ‘Device”
manoj shivhare
Authorhey guys, i am facing problem..
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[AppDelegate managedObjectContext]:
Sheetal Shinde
AuthorThanks for informative tutorial!!!!