Core Data Tutorial for iOS: Getting Started

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Core Data Tutorial for iOS: Getting Started

Postby rwenderlich » Tue Apr 13, 2010 7:00 am

This is the official thread to discuss the following blog post: Core Data Tutorial for iOS: Getting Started
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2144
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 332 times

Re: Core Data Tutorial: Getting Started

Postby kamlesh » Wed Dec 29, 2010 12:51 pm

Hi Ray,
Thanks for such a simple but effective tutorial....
I am fairly new to iPhone development.I have managed to store the data into the entity and retrieve it.But the thing is,every time the application launches,the data is again saved into it.How do I check that if the data is already present then proceed without saving???...


Thanks in advance
kamlesh
n00b
 
Posts: 1
Joined: Wed Dec 29, 2010 12:46 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby RWGoldwin » Sun Jan 02, 2011 8:02 pm

Hi and thanks for a great tutorial. Much better than the book I recently read.
I downloaded the project code, but I couldn't compile.
I am rather new to IPhone development and it looks like I don't have Base SDK for version 3.1.3. I only have from version 3.2 and up.
How do I convert your project to a newer version of SDK?
RWGoldwin
n00b
 
Posts: 1
Joined: Sun Jan 02, 2011 7:58 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby rwenderlich » Mon Jan 03, 2011 7:06 pm

@kamlesh: You can run a query to see if the data already exists. One simple way is just to run a count against how many rows are in the DB, and if it's 0 you know you need to insert your sample data, otherwise it's already there.

@RWGoldwin: To compile the code on the latest SDK, just go to Project\Edit Project Settings, pick the Build Tab, and change the Architectures\Base SDK setting to "Latest IOS".
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2144
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 332 times

Re: Core Data Tutorial: Getting Started

Postby bucanero » Tue Jan 25, 2011 12:41 pm

Hi Ray...my efforts in learning xcode would be wasted without your tutorials!
i'm trying to write an app with a read only database...the structure is: 2 entity in core data (a general view and a detailed one), the problem is that the detailView should contain a photo, a link, maybe coordinates and (in future) a midi file..how can i prefill my DB with all these stuff? i'm sure core data is the right way to organize my data, but i can't really figure out how to prepare the database (about 200 elements) for my future users to enjoy it!

can i give you a suggestion? why don't you prepare one of your fantastic tutorial on core location or map kit (even better on both interacting!!)

thank you so much for your work!!

Luca from Italy
bucanero
n00b
 
Posts: 1
Joined: Tue Jan 25, 2011 12:31 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby Raconteur » Fri Feb 04, 2011 10:05 pm

Hi Ray,

Great tut! Thanks.

I am trying to get the details view integrated, which I do not see in the CD tutorial, but am having some strange problems.

First off, what I did was add the FailedBankDetailViewController classes (.h, .m, and the .xib). Added the labels to the XIB and linked them to the their respective outlets in the ViewController class. That all works just fine.

Next, I added to the FailedBanksListViewController.h:
Code: Select all
// above @interface
@class FailedBankDetailViewController;

// inside @interface
FailedBankDetailViewController *detailViewController;

// after @interface
@property (nonatomic, retain) FailedBankDetailViewController *detailViewController;


in FailedBankListViewController.m:
Code: Select all
// before @implementation
#import "FailedBankInfo.h"
#import "FailedBankDetailViewController.h"

// inside @implementation
@synthesize detailViewController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.detailViewController == nil) {
      self.detailViewController = [[[FailedBankDetailViewController alloc] initWithNibName:@"FailedBankDetailViewController" bundle:nil] autorelease];
   }
   
   FailedBankInfo *info = [self.failedBankInfoList objectAtIndex:indexPath.row];
   detailViewController.model = info;
   [self.navigationController pushViewController:self.detailViewController animated:YES];
}

//  I also added the requisite dealloc and viewDidUnload cleanup


Then, the FailedBankDetailViewController.h looks like this:
Code: Select all
#import <UIKit/UIKit.h>
#import "FailedBankInfo.h"
#import "FailedBankDetail.h"

@interface FailedBankDetailViewController : UIViewController {
   UILabel *nameLabel;
   UILabel *cityLabel;
   UILabel *stateLabel;
   UILabel *zipLabel;
   UILabel *acquiringInstituteLabel;
   UILabel *closeDateLabel;
   UILabel *updatedDateLabel;
   FailedBankInfo *model;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *cityLabel;
@property (nonatomic, retain) IBOutlet UILabel *stateLabel;
@property (nonatomic, retain) IBOutlet UILabel *zipLabel;
@property (nonatomic, retain) IBOutlet UILabel *acquiringInstituteLabel;
@property (nonatomic, retain) IBOutlet UILabel *closeDateLabel;
@property (nonatomic, retain) IBOutlet UILabel *updatedDateLabel;
@property (nonatomic, retain) FailedBankInfo *model;

@end


And FailedBankDetailViewController.m looks like:
Code: Select all
#import "FailedBankDetailViewController.h"

@implementation FailedBankDetailViewController

@synthesize nameLabel;
@synthesize cityLabel;
@synthesize stateLabel;
@synthesize zipLabel;
@synthesize acquiringInstituteLabel;
@synthesize closeDateLabel;
@synthesize updatedDateLabel;
@synthesize model;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
 }
 */


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
   
   [self.nameLabel release];
   [self.cityLabel release];
   [self.stateLabel release];
   [self.zipLabel release];
   [self.acquiringInstituteLabel release];
   [self.closeDateLabel release];
   [self.updatedDateLabel release];
   [self.model release];
}


- (void)dealloc {
   [self.nameLabel release];
   [self.cityLabel release];
   [self.stateLabel release];
   [self.zipLabel release];
   [self.acquiringInstituteLabel release];
   [self.closeDateLabel release];
   [self.updatedDateLabel release];
   [self.model release];
   
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {   
   if (self.model != nil) {
      FailedBankDetail *details = self.model.details;
      
      [self.nameLabel setText:self.model.name];
      [self.cityLabel setText:self.model.city];
      [self.stateLabel setText:self.model.state];
      [self.zipLabel setText:details.zip];
      [self.acquiringInstituteLabel setText:details.acquiringInstitution];
      
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      [formatter setDateFormat:@"MMMM dd, yyyy"];
      
      NSDate *closeDate = details.closeDate;
      NSDate *updatedDate = details.updatedDate;
      
      [self.closeDateLabel setText:[formatter stringFromDate:closeDate]];
      [self.updatedDateLabel setText:[formatter stringFromDate:updatedDate]];
   }
}

@end


Everything looks good to me, and it all plays well, except...

When I click a row to view the details, the closeDate and updatedDate are both December 31, 2000. The data in the DB is correct, and I get the correct zipcode and acquiringInstitution data, so I know I am hitting the right object.

The problem is I cannot inspect into the managed class, so I cannot see where the problem lies, and it is driving me nuts.

Can you offer any insight or clues as to where this might be failing?

Just as an addendum, my DB has the two date fields set as TEXT, and they contain data such as:
2011-01-28 00:00:00
2011-01-20 00:00:00

Thanks a ton for the tuts, and for any help!

Cheers,

Chris
Raconteur
Baby Hacker
 
Posts: 5
Joined: Fri Feb 04, 2011 9:28 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby JohnGoodstadt » Sun Feb 13, 2011 2:52 pm

Thanks for the tutorial - I found that it cleared up some confusion. Of course our projects are different - so seeing a standard one helps to understand how to vary it. For example I have a 1 to Many relationship - and the delete cascade was not working. Thanks to Tom C - for the answer about assigning the relationships and NSSET - now the Delete Cascade works.

A couple of typos in the HTML page for this blog

"So we go ahead adn set up a test bank," ADN instead of AND

and

"objects are just modified in meomry" meomry instead of memory
JohnGoodstadt
n00b
 
Posts: 2
Joined: Sun Feb 13, 2011 2:36 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby ApopheniaOverload » Sun Feb 20, 2011 5:26 am

Quick question regarding the NSSet issue. It seems like NSSet* is the default for to-many relationships. So say FailedBankInfo had a to-many relationship with FailedBankDetails. In that case, in FailedBankInfo, should we just have the relationship variable details be an NSSet* object, instead of a Detail object?

Edit: Actually, now that I think about it, logically only an NSSet* of Detail objects makes sense.
ApopheniaOverload
n00b
 
Posts: 1
Joined: Sun Feb 20, 2011 5:09 am
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby DavidK » Fri Feb 25, 2011 4:43 pm

I have succesfully completed your example for populating a table from Coredata .I am now trying to use the same code to Populate a Tableview which has an Index and Search included.My problem occurs because of MutableDictionary,I can load the records into allNames (Dictonary) and names (MutableDictionary).However when resetSearch is called the program bombs out at the line.

NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];

error is

unrecognized selector sent to instance 0x643fae0

This is doing my head in, as I thought I was on the 'Home straight' with my first App
DavidK
n00b
 
Posts: 1
Joined: Fri Feb 25, 2011 4:39 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Core Data Tutorial: Getting Started

Postby BigSprocket » Fri Feb 25, 2011 5:30 pm

@DavidK NSMutableDictionary doesn't support mutableDeepCopy natively. But, you can accomplish it with a category ... check out this page from the Mark/LaMarche book.
http://books.google.com/books?id=TcP2bg ... dip9IsvRCP
____________
Kyle Davis, Head Geek, BigSprocket -- Spread knowledge, get paid @ LetsTalkCocoa.com
User avatar
BigSprocket
Hacker
 
Posts: 18
Joined: Mon Feb 21, 2011 4:25 pm
Location: Katy, TX
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: Bing [Bot] and 12 guests