Core Data Strings - iphone

Im new to using core data and having really basic problems. Im trying to have the user enter a string and then be able to save that string and allow it to be returned to them at some point. But i cannot seem to get it to save. In fact the program quits when I attempt to run the following method. I can post the rest of my project, but i thought maybe that would be annoying so let me know if seeing it in greater detail would help. Thanks so much.
James
.h: file
#import <UIKit/UIKit.h>
#import "People.h"
#class rootViewController;
#interface data : UIView <UITextFieldDelegate>{
rootViewController *viewController;
UITextField *firstName;
UITextField *lastName;
UITextField *phone;
UIButton *saveButton;
NSMutableDictionary *savedData;
//Used for Core Data.
NSManagedObjectContext *managedObjectContext;
NSMutableArray *peopleArray;
}
#property (nonatomic, assign) rootViewController *viewController;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) NSMutableArray *eventArray;
- (id)initWithFrame:(CGRect)frame viewController:(rootViewController *)aController;
- (void)setUpTextFields;
- (void)saveAndReturn:(id)sender;
- (void)fetchRecords;
#end
.m file:
-(void)saveAndReturn:(id)sender{
People *userEnteredName = (People *)[NSEntityDescription insertNewObjectForEntityForName:#"People" inManagedObjectContext:managedObjectContext];
[userEnteredName setName:firstName.text];
//NSError *error;
//if (![managedObjectContext save:&error]) {
// This is a serious error saying the record could not be saved.
// Advise the user to restart the application
//}
[peopleArray insertObject:userEnteredName atIndex:0];
}

From the error you gave you must have named the People object differently - in the model are you using "People" for both class and entity name (those can be the same)?
Edit:
After reviewing your code, you had multiple problems:
1) In the app delegate you did "[data alloc]" but no init. That was where you set the managed object context, but it was never used... not just because of the lack of an init but because...
2) The place where the data controller was really built and used from was the rootViewController. That's the one that is actually doing all the work, the one in the app delegate is just discarded.
3) So where to get the context then? Honestly the best spot is in the data controller, one fix I know worked was putting this line before every time the context was accessed:
#import "UserProfileAppDelegate.h"
// Then in the method before the use of context........
self.managedObjectContext = [((UserProfileAppDelegate *)[[UIApplication sharedApplication] delegate]) managedObjectContext];
When that was in place, the project ran. I think though you should put that into something like a viewDidLoad on the data controller (if it has a view that is ever used).

Related

I'm trying to pass a string from my first ViewController to my second ViewController but it returns NULL

In my first view controller I have 3 input fields each of them take the user input into and saves it into a string such as: address, username and password as NSUserDefaults. This part works fine.
In my second view controller I'm trying to take the 3 strings from first controller (address, username and password) create a html link based on the 3 strings. I've tried many ways to access the 3 strings with no luck, the result I get is NULL.
Here is my code:
//.h file - first view controller with the 3 input fields CamSetup.h
#import <UIKit/UIKit.h>
#interface CamSetup : UIViewController <UITextFieldDelegate>
{
NSString * address;
NSString * username;
NSString * password;
IBOutlet UITextField * addressField;
IBOutlet UITextField * usernameField;
IBOutlet UITextField * passwordField;
}
-(IBAction) saveAddress: (id) sender;
-(IBAction) saveUsername: (id) sender;
-(IBAction) savePassword: (id) sender;
#property(nonatomic, retain) UITextField *addressField;
#property(nonatomic, retain) UITextField *usernameField;
#property(nonatomic, retain) UITextField *passwordField;
#property(nonatomic, retain) NSString *address;
#property(nonatomic, retain) NSString *username;
#property(nonatomic, retain) NSString *password;
#end
//.m file - first view controller CamSetup.m
#import "CamSetup.h"
#interface CamSetup ()
#end
#implementation CamSetup
#synthesize addressField, usernameField, passwordField, address, username, password;
-(IBAction) saveAddress: (id) sender
{
address = [[NSString alloc] initWithFormat:addressField.text];
[addressField setText:address];
NSUserDefaults *stringDefaultAddress = [NSUserDefaults standardUserDefaults];
[stringDefaultAddress setObject:address forKey:#"stringKey1"];
NSLog(#"String [%#]", address);
}
-(IBAction) saveUsername: (id) sender
{
username = [[NSString alloc] initWithFormat:usernameField.text];
[usernameField setText:username];
NSUserDefaults *stringDefaultUsername = [NSUserDefaults standardUserDefaults];
[stringDefaultUsername setObject:username forKey:#"stringKey2"];
NSLog(#"String [%#]", username);
}
-(IBAction) savePassword: (id) sender
{
password = [[NSString alloc] initWithFormat:passwordField.text];
[passwordField setText:password];
NSUserDefaults *stringDefaultPassword = [NSUserDefaults standardUserDefaults];
[stringDefaultPassword setObject:password forKey:#"stringKey3"];
NSLog(#"String [%#]", password);
}
- (void)viewDidLoad
{
[addressField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey1"]];
[usernameField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey2"]];
[passwordField setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey3"]];
[super viewDidLoad];
}
#end
//.h second view controller LiveView.h
#import <UIKit/UIKit.h>
#import "CamSetup.h"
#interface LiveView : UIViewController
{
NSString *theAddress;
NSString *theUsername;
NSString *thePassword;
CamSetup *camsetup; //here is an instance of the first class
}
#property (nonatomic, retain) NSString *theAddress;
#property (nonatomic, retain) NSString *theUsername;
#property (nonatomic, retain) NSString *thePassword;
#end
//.m second view LiveView.m file
#import "LiveView.h"
#interface LiveView ()
#end
#implementation LiveView
#synthesize theAddress, theUsername, thePassword;
- (void)viewDidLoad
{
[super viewDidLoad];
theUsername = camsetup.username; //this is probably not right?
NSLog(#"String [%#]", theUsername); //resut here is NULL
NSLog(#"String [%#]", camsetup.username); //and here NULL as well
}
#end
There are 5 issues in this code:
If you are using ARC, all the "retain" in your #properties should be changed to "strong"
You name your iVars and properties the same thing. (common bad practice)
You are always directly accessing iVars and not properties in your code.
You don't retain your instance of CamSetup in the second object.
The direct cause of your problem: in the second object you've only created a placeholder for a CamSetup instance, you've not created one nor passed one to it! self.camSetup in your second object is empty right now.
Let's go step by step:
First, give your iVars different names from your properties. This is best practice, especially for a beginner! :)
#interface CamSetup : UIViewController <UITextFieldDelegate>
{
NSString *_address;
}
#property(nonatomic, strong) NSString *address;
#end
#implementation CamSetup
#synthesize address=_address;
...
#end
This is important, because you've setup properties, but in your code, you are not using them, you are directly accessing your iVars. Since you've named them the same thing, you might not see this.
Let's look at your first object. Every "address" in your code is going to your iVar and not property. Generally, you want to access the iVars via your properties unless you're sure otherwise. The #synthesize creates a getter and setter method for your iVar that will retain the var because you told it to in your #property statement. However, when you directly access your iVar's you're not going through those accessors and thus the stuff you wrote in your #properties doesn't matter. You could end up misunderstanding a lot of errors and bugs if you aren't clear about this. In your first object this worked anyway because the alloc/init sets a retain on the object, but I noticed you always do this, and that's going to get you into trouble.
Here's what the saveAddress: method would look like using properties:
-(IBAction) saveAddress: (id) sender
{
self.address = [[NSString alloc] initWithFormat:self.addressField.text];
[self.addressField setText:self.address];
NSUserDefaults *stringDefaultAddress = [NSUserDefaults standardUserDefaults];
[stringDefaultAddress setObject:self.address forKey:#"stringKey1"];
NSLog(#"String [%#]", self.address);
}
Next, in your second object you need to set properties for the CamSetup instance! Right now, you just have an iVar.
#import <UIKit/UIKit.h>
#import "CamSetup.h"
#interface LiveView : UIViewController
{
NSString *_theAddress;
NSString *_theUsername;
NSString *_thePassword;
CamSetup *_camSetup; //here is an instance of the first class
}
#property (nonatomic, retain) CamSetup *camSetup; // in synthesize we'll specify that this property uses the _camSetup iVar
#property (nonatomic, strong) NSString *theAddress;
#property (nonatomic, strong) NSString *theUsername;
#property (nonatomic, strong) NSString *thePassword;
#end
The implementation:
#import "LiveView.h"
#interface LiveView ()
#end
#implementation LiveView
#synthesize camSetup = _camSetup;
#synthesize theAddress = _theAddress;
#synthesize theUsername = _theUsername;
#synthesize thePassword = _thePassword;
- (void)viewDidLoad
{
[super viewDidLoad];
self.theUsername = self.camSetup.username; //this is probably not right?
// well, it's better now, but where is camSetup coming from??? it's undefined now
NSLog(#"String [%#]", self.theUsername); //resut here is NULL
NSLog(#"String [%#]", self.camSetup.username); //and here NULL as well
}
#end
We've created an iVar and property pair that will hold a pointer to a CamSetup instance and will retain that pointer (if we set it using the property). However, where is that CamSetup instance being created? Where do you alloc/init it?
There are many possible answers to this question. If CamSetup had getters for address, username, password that read them back in from your user defaults, then all you'd have to do is alloc/init a CamSetup and set it to camSetup. However, right now your first object has no functionality to retrieve the saved data so we can't do that. (still, this is the solution I'd hope you'd implement).
You might be initializing both of these in your app delegate? However, if you are using storyboard then likely it is initializing these object for you when it initializes your interface. In this case, in your appDelegate app has finished launching... method, you'll have to retrieve pointers to these instances, then the camSetup property on your second object, to point to the first. To tell you how to do this, we'd have to know detailed specifics of your app. Still, this wouldn't be doing it the best way.
Best practice would be to create an object which saves and retrieves these data from user defaults for you. This future proofs your implementation should you later want to change the way you store these data. You'd just change the way they are stored/retrieved within their class.
Your problem is here:
CamSetup *camsetup; //here is an instance of the first class
You aren't doing anything to make camsetup refer to the same instance of the CamSetup class that is taking input from the user. The camsetup variable is never initialized, hence it's NULL, hence any properties you try to retrieve from it will also be NULL.
How exactly you'd fix this depends on the structure of your program. One possibility is to have whatever code is creating the LiveView controller set the camsetup value. For example:
LiveView *liveViewController = [LiveView alloc] initWithNibName:#"LiveView" bundle:nil]];
liveViewController.camsetup = camSetupController;
(you'd need to make camsetup a property to do this).
BUT, from a design standpoint, having the one view controller have a reference to the other is probably the wrong way to go about solving this problem -- it introduces unnecessary dependencies between the two controllers. For example, say you later decide to make it possible to go directly to the LiveView controller upon program launch, using a saved name/password; you can't do that if LiveView depends on getting its input from a CamSetup object.
So, a better approach might be to have LiveView take its input from NSUserDefaults, and/or by having the code that's calling LiveView set the username/password properties.
-- edit --
For example, retrieve the data from the user defaults like:
self.address = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey1"];
Is the camsetup initialized? If not -- initialize it first.
There are many ways to do this, most of which depend on how the two controllers are related to each other (like how are you going from the first to the second).
However, in your case you don't need to pass the string at all, since you are saving them in user defaults. Just read the values of those defaults in your second controller.
Hi i got your problem just do one thing.
When you are setting value in NSUserDefaults it need synchronization so just synchronize it.
[stringDefaultUsername synchronize];
And retrieve data using
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringKey2"];
Follow apple NSUserDefaults Class Reference for more info

Property not found in object type

I am working on a simple core data app that uses a tableview and a detail view. I am getting the error message stating that the property managedObjectContext is not found in the object type ChildrenTVC. The problem is that it really is there. I have cleaned the project and deleted the derived data. There must be something else going on.
Here is the code for the object header:
#interface ChildrenTVC : CoreDataTableViewController <AddChildTVCDelegate>
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#end
and the code for the implementation file:
#import "ChildrenTVC.h"
#implementation ChildrenTVC
#synthesize managedObjectContext = _managedObjectContext;
#synthesize fetchedResultsController = _fetchedResultsController;
Here is the app delegate file where the error registers:
#import "AppDelegate.h"
#import "ChildrenTVC.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize managedObjectModel = __managedObjectModel;
#synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ( NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
ChildrenTVC *controller = (ChildrenTVC *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
The error is at the controller.managedObjectContext. A lot of this is boilerplate code so there is not much to it. I just can't figure out why it is not seeing the property for the managed object context. The property is in the code.
UPdate:
I ended up recreating the file entirely. I am not sure what references are still in place when a file is changed, but something was pointing to the wrong file. I had to empty the trash to get the new file work properly. It seems to work now, though. All the research I did ended up revealing that there are some things that are unexplained in Xcode.
I had solved this issue by entirely recreating the header file, but I never really understood why it happened. What I did not understand then were the effects of the changes that I had made to the app. The real problem was an issue in the sequence that the header files were imported. I had changed the #import in two implementation files and that caused the compiler to not read one of the header files. I could see that the code was there, but the compiler could not read the code because it was not importing it. That gave way to the error I was receiving. My solution simply reversed the change that I had made. It would more easily have been solved by simply removing the #import of the view controller header file on the app delegate. I recently tested this and it was the correct solution.
You are type casting the navigationController.topViewController but i assume it isn't one in reality. Try this
ChildrenTVC *controller = [[ChildrenTVC alloc] init];
NSArray *vcArray = NSArray *vcArray = [NSArray arrayWithObject:controller]:
[self.window.rootViewController setViewControllers:vcArray animated:NO];
What happens if you make following changes to ChildrenTVC.h
#interface ChildrenTVC : CoreDataTableViewController <AddChildTVCDelegate>
{
NSManagedObjectContext *managedObjectContext;
NSFetchedResultsController *fetchedResultsController;
}
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#end

where to implement Core Data?

I´m completely new to core data programming. i just try to find out where the best place for implementing the core data code would be. i´ve done the apple tutorial Locations and it worked well. now i try to transfer that to my current project what is a bit more complex.
the Locations tutorial shows one RootViewController including a programmatically generated tableView. my project is based on a tabView template. it owns a MainWindow.xib including the TabBarController including three ViewController (MapView, ListView, SettingsView) where each view has it´s own navigationController and xib-file.
The first stumbling block was changing the code that it will run with a xib for the tableView instead of creating it programmatically. I´ve managed that but there is still one error. I can´t connect the managedObjectContext from the appDelegate to the listViewController. I´ve tried the examples and suggestions for that issue from this forum here. but it still doesn´t work.
after looking at the CoreDataBooks sample project i´ve seen that the core data code was implemented in the RootViewController as well. Seems that it would be the wrong way to implement it in the ListViewController. But i don´t have a RootViewController in my project. In the AppDelegate i directly pass the tabBarController as the rootViewController. therefore i don´t know how to reach the listViewController to set the context like it was done in the Locations sample.
As the MapView is the first view i can´t set the context in the appDelegate. And after struggling a long time with the managedObjectContext i wonder if it would be better to invent a RootViewController to be able to place additional code there. the model should be accessible by all three views and it seems that the RootViewController is the right place.
But how do i combine that with a tabBarController which includes three more viewControllers based on xib-files? Could somebody recommend me examples or tutrials including core data based on a tab bar app?
Please read the following article by Marcus Zarra: Passing around a NSManagedObjectContext on iOS. That should give you an idea how to solve your problem.
In general you should add a NSManagedObjectContext property to all of your ViewControllers and pass the context before adding them to the view stack via pushViewController:animated:. You should not take the context from your app delegate.
If you pass a single NSManagedObject to a ViewController, e.g. to present a kind of detail view, you can access the context from that object, as every NSManagedObject knows about the NSManagedObjectContext it is "living" in.
If you are a registered iOS developer, I'd also recommend the WWDC 2010 and 2011 videos. There are some sessions about mastering Core Data.
ok, now i have the correct solution. it took a while to understand but now it works with dependency injection from application delegate into the view controllers (listViewController).
my problem was that i didn´t know how to reference my view controllers as they are nested into dedicated navControllers and one tabBarController.
after reading a lot of postings here i understood i have to declare my view controllers in the appDelegate.h and synthesize them in appDelegate.m and after that connect them to the appropirate item in IB. that was done fast & easy after understanding :-)
there is no rootViewController needed.
MyAppDelegate.h:
#import <UIKit/UIKit.h>
#import "ListViewController.h"
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
IBOutlet ListViewController *listViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet ListViewController *listViewController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
MyAppDelegate.m:
#import "MyAppDelegate.h"
#import "ListViewController.h"
#implementation MyAppDelegate
#synthesize window=_window;
#synthesize tabBarController=_tabBarController;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
#synthesize listViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
}
// Pass the managed object context to the view controller.
listViewController.managedObjectContext = context;
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
return YES;
}
...
ListViewController.h
#import <CoreLocation/CoreLocation.h>
#interface ListViewController : UITableViewController <CLLocationManagerDelegate> {
UINavigationController *navController;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
-(NSManagedObjectContext *)managedObjectContext;
#end
ListViewController.m
#import "MyAppDelegate.h"
#import "ListViewController.h"
#implementation ListViewController
#synthesize navController;
#synthesize managedObjectContext;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"managedObjectContext: %#",[self managedObjectContext]);
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(#"error: %#",[self managedObjectContext]);
return;
}
...
I've coded an app like that some time ago. The way I've solved it is I made a singleton which had a persistentStoreCoordinator property like the one in Apple documentation to hold the access to the database (so I don't have to write it every time). Then in every tab bar view controller I've initiated its own NSManagedObjectContext.
NSPersistentStoreCoordinator *coordinator = [[Singleton sharedSingleton] persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
That way every controller approaches the database with it's own context, if you understand what I mean.
Please note that if any of your view controllers has a detail view controller, take the standard approach of passing the managed object context to it like in sample code (Books, Locations, Recipes).
i just fixed the bug. i missed out some methods necessary in the appDelegate. it works now if i put following code into viewDidLoad of my ListViewController
if (managedObjectContext == nil) {
NSLog(#"managedObjectContext is nil");
managedObjectContext = [(IntraAppAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
is that OK concerning proper MVC pattern rules? in my case the ViewController takes the context from the appDelegate now.
trying to set the context in the appDelegate with something like that throws an error:
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
}
// Pass the managed object context to the view controller.
self.tabBarController.listViewController.navController.managedObjectContext = context;
self.window.rootViewController = self.tabBarController;
how can i gather the reference of other viewControllers which are controlled by the tabBarController and are not the topView/superView after app start? the first view is the MapView. do i have to instantiate or declare the listViewController in appDelegate? how must it be coded that it referes to the listViewController controlled by the tabBarController?

Release of NSManagedObject

I have based a portion of an app on Apple's CoreDataRecipes example code attainable at
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
After some modifications I spent a good few hours tracking down a bug which I must have introduced, but which I solved by removing two lines of code present in apple's code.
I added an author attribute to the NSManagedDataObject recipe, identical in implementation - as far as I could tell - to other string attributes which recipe already had. My new attribute became a zombie after entering and leaving the modal view controlled by IngredientDetailViewController. The dealloc method of IngredientDetailViewController was
- (void)dealloc {
[recipe release];
[ingredient release];
[super dealloc];
}
Having tracked down the bug, I commented out the releases on the recipe and the ingredient (another NSManagedObject) and my app now seems to be functioning. I have now discovered that my code works with or without those release calls; the bug must have been fixed by another change I made. I am now wondering
Why was apple's example code written like this originally?
What was it about the original attributes of the NSManagedObject recipe which meant that they were not susceptible to zombification from the dealloc calls?
If the above hasn't displayed my ignorance enough, I should point out that I am new to Objective C and iPhone development but I would really like to understand what's going on here.
EDITED IN RESPONSE TO COMMENTS AND UPDATED:
I now cannot replicate the zombie creation by uncommenting those lines, obviously another change during bugshooting did the trick. Some of what I originally asked is now invalid but this has left me further confused as to the use of release for NSManagedObjects, since now functionality seems identical with or without those calls. My main question now is just whether or not they should be there. The crash was occuring upon saving in the IngredientDetailView. Here is the header:
#class Recipe, Ingredient, EditingTableViewCell;
#interface IngredientDetailViewController : UITableViewController {
#private
Recipe *recipe;
Ingredient *ingredient;
EditingTableViewCell *editingTableViewCell;
}
#property (nonatomic, retain) Recipe *recipe;
#property (nonatomic, retain) Ingredient *ingredient;
#property (nonatomic, assign) IBOutlet EditingTableViewCell *editingTableViewCell;
#end
and the save method:
- (void)save:(id)sender {
NSManagedObjectContext *context = [recipe managedObjectContext];
/*
If there isn't an ingredient object, create and configure one.
*/
if (!ingredient) {
self.ingredient = [NSEntityDescription insertNewObjectForEntityForName:#"Ingredient"
inManagedObjectContext:context];
[recipe addIngredientsObject:ingredient];
ingredient.displayOrder = [NSNumber numberWithInteger:[recipe.ingredients count]];
}
/*
Update the ingredient from the values in the text fields.
*/
EditingTableViewCell *cell;
cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
ingredient.name = cell.textField.text;
cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
ingredient.amount = cell.textField.text;
/*
Save the managed object context.
*/
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate.
You should not use this function in a shipping application, although it may be useful during development.
If it is not possible to recover from the error, display an alert panel that instructs the user to quit the
application by pressing the Home button.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self.navigationController popViewControllerAnimated:YES];
NSLog(#"in ingredient detail save after ingredient pop; - recipe.author is %#", recipe.author);
}
since I'm a new user I can't put the screenshot of the data model here, so here is a link to it: data model screenshot
and finally the Recipe header:
#interface ImageToDataTransformer : NSValueTransformer {
}
#end
#interface Recipe : NSManagedObject {
}
#property (nonatomic, retain) NSString *instructions;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *overview;
#property (nonatomic, retain) NSString *prepTime;
#property (nonatomic, retain) NSSet *ingredients;
#property (nonatomic, retain) UIImage *thumbnailImage;
#property (nonatomic, retain) NSString *author;
#property (nonatomic) BOOL *isDownloaded;
#property (nonatomic) BOOL *isSubmitted;
#property (nonatomic, retain) NSString *uniqueID;
#property (nonatomic) float averageRating;
#property (nonatomic) float numberOfRatings;
#property (nonatomic, retain) NSManagedObject *image;
#property (nonatomic, retain) NSManagedObject *type;
#end
#interface Recipe (CoreDataGeneratedAccessors)
- (void)addIngredientsObject:(NSManagedObject *)value;
- (void)removeIngredientsObject:(NSManagedObject *)value;
- (void)addIngredients:(NSSet *)value;
- (void)removeIngredients:(NSSet *)value;
#end
Thanks again.
Please take a look at Core Data documentation, since Core Data “owns” the life-cycle of managed objects you should not be releasing them at all.
The only time you would release a managed object would be if you had retained it yourself. Seeing as your property definition says that it is retaining the recipe and ingredient objects, when your ingredientviewcontroller is deallocated, it needs to release the recipe and ingredient objects.
When you do something like myIngredientViewController.ingredient = anIngredient, it's like calling a method which would look something like:
- (void)setIngredient:(Ingredient *)ing {
[self willChangeValueForKey:#"ingredient"];
Ingredient *oldIngredient = ingredient;
ingredient = [ing retain];
[oldIngredient release];
[self didChangeValueForKey:#"ingredient"];
}
So in your save method, when it assigns self.ingredient = ..., that is retaining your object yourself - you now have an ownership interest in that object, so you need to release that in your dealloc.
If you think about it in another way, the managed object context has added 1 to the retain count because it has an ownership interest in it, and you have added 1 to the retain count because you want to maintain an ownership interest in it. When you relinquish your ownership interest, by releasing it during dealloc, the retain count goes down 1 and when the managed object context releases it, the retain count would go to zero and it would be deallocated.
That is how normal objects operate, and how you would treat managed objects in most circumstances, but there are a few caveats for managed objects - as the previous poster indicated, the lifecycle of managed objects is controlled by the managed object context, and there are various things that can happen to a managed object that may mean that although the object still exists, it may be deleted in the context, or a fault in the context, or maybe even reused with different data.
You don't usually have to worry about that, but if you use custom managed objects which have their own instance variables that you need to manage the memory for, or other things you want to do when they are created, fetched, turned into faults etc, then you would need to look at the awakeFromInsert, awakeFromFetch, willTurnIntoFault, didTurnIntoFault etc.
But all that is advanced stuff that you won't need until you get into more complex scenarios.
HTH

Access a NSMutableArray of an object

I post this topic because I have a problem with my iPhone application since 3 days. I hope someone can help me because I'm going crazy.
Here is the thing : I fill in an object userXMLData,in the delegate of my application, with a XML Parser. This object contains many NSStrings and a NSMutableArrays which contains objects type Album to.
My problem is : I can display all data of userXMLData with an internal function, but when I'm trying to get the data from the array in my viewController , it doesn't work. I mean, it crashes. It's weird because I can access to the appDelegate.userXMLData.NSString but not of my appDelegate.userXMLData.NSMutableArray
Here is my code :
// Initializaiton in the delegate
userXMLData = [[UserXMLData alloc] init];
userXMLData.myArray = [[NSMutableArray alloc] init];
UserXMLData.h
#interface UserXMLData : NSObject {
// User Data
NSString *userId;
// Content
NSMutableArray *myArray;
}
#property(nonatomic, retain) NSString *myString;
#property(nonatomic, copy) NSMutableArray *myArray;
#end
//Album.h
#interface Album : NSObject {
NSString *albumId;
NSMutableArray *content;
}
#property(nonatomic, retain) NSString *albumId;
#property(nonatomic, retain) NSMutableArray *content;
#end
As I said, I really don't why it crashes. I'm stuck and I cannot continue my application without fixing it.
Enable Zombies by following the instructions here:
http://loufranco.com/blog/files/debugging-memory-iphone.html
This will cause your app to not release any objects and instead cause them to complain to the console if messages are sent to them after they are released.
The most common cause of a crash is releasing too often (or retaining too few times).
Also, running a Build and Analyze can sometimes point these out.
Would be able to answer better if you'd show the code where you are trying to access the array and the error you receive on crash, but I'd hazard a guess that you don't have #synthesize myArray in your implementation (.m) file