0

私はいくつかのViewControllerを持っています。ログインする最初のLoginViewControllerで、newEntityを作成します。「ログイン」ボタンをクリックすることにより、フェッチを行い、述語「ログイン」によってこのnewEntityフェッチを割り当てます。AppDelegate.hは、CoreData用のものを作成しました。LoginViewControllerでは、.hに次の文字列があります。

@class AppDelegate;
...
@property (strong, nonatomic) AppDelegate *appMeDelegate;
@property (strong, nonatomic) NSManagedObjectContext *context;
...

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();
}}

そして、他のViewControllerでnewEntityを使用しますが、保存できません

- (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];

しかし、結果はなく、エンティティの変更を保存しませんでした

4

1 に答える 1

1

このようなものを使用して managedDocument コンテキストを保存します

    [self.managedDocument saveToURL: self.managedDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
    if (completionHandler) completionHandler(success);
}];

EDIT 申し訳ありませんが、明らかに NSManagedDocument を使用していないことがわかりました(これを調べる必要があります)。

これをしないでください:

[self.loginViewController saving];

以前と同じようにしてください:

appMeDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = appMeDelegate.managedObjectContext;
[context save: &error];
于 2012-12-17T20:43:23.233 に答える