I have some ViewControllers. In the first LoginViewController, where I login, I create newEntity. By clicking on button “Login” I make fetch and assign for this newEntity fetch by predicate “login”. AppDelegate.h have create things for Core Data. In LoginViewController I have next strings in .h:
@class AppDelegate;
...
@property (strong, nonatomic) AppDelegate *appMeDelegate;
@property (strong, nonatomic) NSManagedObjectContext *context;
...
In LoginViewController.m
#import "AppDelegate.h"
...
@synthesize ... appMeDelegate, context;
...
//I have login button where i compare login and password in
//UITextFields with that in Core Data and if it is
//all right i load fetch for user with his login
-(IBAction)login:
{
appMeDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
context = appMeDelegate.managedObjectContext;
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc]init];
NSPredicate *predicateMe = [NSPredicate predicateWithFormat:@"login==%@",login.text];
fetchRequest.predicate = predicateMe;
... //then I create newEntity }
//next method for saving
-(void)saving
{
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}}
And then in other ViewController i use newEntity but can not save it
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *newdata = [NSData dataWithData:self.loginViewController.newEntity.photo];
photoArray = [NSMutableArray arrayWithArray:
[NSKeyedUnarchiver unarchiveObjectWithData:newdata]];}
-(IBAction)save
{ [photoArray addObject:myImage];
NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:photoArray];
[self.loginViewController.newEntity setValue:newData forKey:@"photo"];
[self.loginViewController saving];
But no results and it didn’t save changing in entity
You would use something like this to save your managedDocument context
EDIT
Sorry, just saw you are obviously not using an NSManagedDocument (which you should look into).
Dont do this:
Do it like you did before: