无法创建UIManagedDocument

3
我正在尝试使用Core Data制作iPhone应用程序。我必须使用NSManagedObjectContext来访问数据,为此我使用UIManagedDocument。但是,如果我尝试使用UIManagedDocument创建文档,则文档的openWithCompletionHandler不成功。这就是为什么我的NSManagedObjectContext始终为nil,并且Xcode说无法在etc处创建文档。以下是类:

AddUserViewController.h

#import <UIKit/UIKit.h>

@interface AddUserViewController : UIViewController

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong) UIManagedDocument *document;

@end

AddUserViewController.m

#import "AddUserViewController.h"
#import "User.h"
#import "User+Create.h"

@interface AddUserViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *ageField;
@property (weak, nonatomic) IBOutlet UITextField *sexField;
@property (weak, nonatomic) IBOutlet UITextField *weightField;
@property (weak, nonatomic) IBOutlet UITextField *activityField;


@end

@implementation AddUserViewController

-(void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext{
        _managedObjectContext = managedObjectContext;
}



-(void)createOrWriteDocument{
    NSURL *url = [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask]firstObject];
    url = [url URLByAppendingPathComponent:@"Activities"]; // edited mistakenly deleted
    self.document = [[UIManagedDocument alloc] initWithFileURL:url];

    if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) [self documentIsReady];
            if (!success){
                NSLog(@"could not open document at %@",url);
            }

        }];


    } else {
        [self.document saveToURL:url
           forSaveOperation:UIDocumentSaveForCreating
          completionHandler:^(BOOL success) {
              if (success) {
                  [self documentIsReady];
              }
              if (!success){
                  NSLog(@"could not create document at %@",url);
              }
          }];
    }

}


- (void)documentIsReady
{
    if (self.document.documentState == UIDocumentStateNormal) {
        self.managedObjectContext = self.document.managedObjectContext; 
    } }

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (!self.managedObjectContext) {
        [self createOrWriteDocument];
    }

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];
}

-(void)dismissKeyboard{
    [self.nameField resignFirstResponder];
    [self.ageField resignFirstResponder];
    [self.sexField resignFirstResponder];
    [self.weightField resignFirstResponder];
    [self.activityField resignFirstResponder];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"setUser:"]) {

        //User *user = [[User alloc]init];
        User *user = [User createUserWithname:self.nameField.text
                                      withAge:[NSNumber numberWithDouble:[self.ageField.text doubleValue]]
                                      withSex:self.sexField.text
                                   withWeight:[NSNumber numberWithDouble:[self.weightField.text doubleValue]]
                                 withActivity:self.activityField.text
                       inManagedObjectContext:self.managedObjectContext];

        if ([segue.destinationViewController respondsToSelector:@selector(setUser:)]) {
            [segue.destinationViewController performSelector:@selector(setUser:) withObject:user];

        }
    }

}

@end

编辑

我解决了这个问题。问题出在以下一行:NSURL *url = [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask]firstObject];。我把NSDocumentationDirectory改成了NSDocumentDirectory,问题得到解决。


我正在开发的应用程序中使用UIManagedDocument,唯一的主要区别是我将我的文档名称附加到URL路径的末尾。我还从fileManager中获取lastObject。例如:NSURL * managedDocumentURLPath = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];managedDocumentURLPath = [managedDocumentURLPath URLByAppendingPathComponent:@"AppNameDocument"]; - Jonathan
当我删除了注释行 - 在这里编写代码 - 我也删除了这一行。相同的代码,但在我这里失败了。 - Anamort
2个回答

0

您可以尝试使用以下代码创建您的管理对象上下文。

- (NSManagedObjectContext *) managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return _managedObjectContext;
}

0

请看下面的示例。这对我有效,抱歉没有时间仔细查看以查看差异,我正在使用iCloud选项,因此请删除它们,但尝试设置其他persistentStoreOptions。还请发布您遇到的错误详细信息?

// This gets called when the user has done one of the following:
// 1.  Created a new file and entered a new file name.  We have then created the fileURL
//     using the /Documents directory, the filename and appending '_UUID_'+uuid to ensure that
//     avoid duplicate file names in case the user used the same file name on another device.
// 2.  Selected an existing file from the file browser
//
- (void)createNewFile:(NSURL*)fileURL {

    //FLOG(@"createNewFile called with url %@", fileURL);

    _creatingNewFile = YES; // Ignore any file metadata scan events coming in while we do this because some iCloud
                            // files get created by Core Data before the local files are created and our scanning
                            // picks up new iCloud files and attempts to create local copies and we don't want this
                            // if this devices is busy creating the new iCloud file

    _document = [[OSManagedDocument alloc] initWithFileURL:fileURL];

    // Set oberving on this file to monitor the state (we don't use it for anything other than debugging)
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(documentStateChanged:)
                   name:UIDocumentStateChangedNotification
                 object:_document];

    _openedVersion = [NSFileVersion currentVersionOfItemAtURL:fileURL];
    _openedVersionDate = _openedVersion.modificationDate;
    _openedVersionDevice = _openedVersion.localizedNameOfSavingComputer;
    //FLOG(@" file  version date: %@", _openedVersionDate);
    //FLOG(@" file  version device: %@", _openedVersionDevice);

    NSString *fileName = [[fileURL URLByDeletingPathExtension] lastPathComponent];

    [_document setPersistentStoreOptions:@{NSPersistentStoreUbiquitousContentNameKey:fileName,
                                           NSMigratePersistentStoresAutomaticallyOption:@YES,
                                           NSInferMappingModelAutomaticallyOption:@YES,
                                           NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE" }}];

    _managedObjectContext = _document.managedObjectContext;

    if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
        //FLOG(@" file exists so open it: %@",fileURL);
        [_document openWithCompletionHandler:^(BOOL success){
            if (!success) {
                // Handle the error.
                LOG(@" error creating file");
            } else {
                //LOG(@" file opened");
                [self fileOpened];  // Now initialise the UI and let the user continue...
            }
        }];

    }
    else {
        // File does not exist so that means the user has created a new one and we need to
        // load some initialisation data into the Core Data store (codes tables, etc.)
        //
        // At this stage we have a database in memory so we can just use the _document.managedObjectContext
        // to add objects prior to attempting to write to disk.

        //LOG(@" file DOES NOT exist so add initial data");
        [self addInitialData];

        // Just checking if anything has been written to disk, nothing should not exist on disk yet.
        // Debugging use only
        if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]])
            LOG(@" file exists but keep going anyway :-(");
        else
            LOG(@" file still does not exist :-)");


        // OK now save a copy to disk using UIManagedDocument
        // NOTE: the iCloud files are written before the UIManagedDocument.fileURL, presumably because Core Data does this setup
        // in response to the [moc save:].  Make sure we don't pick this up in our iCloud metaData scan and attempt to create
        // it as if it were a new iCloud file created by some other device.
        //
        [_document saveToURL:_document.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
            if (!success) {
                // Handle the error.
                LOG(@" error saving file :-(");
            }

            // We close the file and wait for it to appear in the file browser and then
            // let the user select it from the browser to open it and start using it.
            // Skip this if you want to open it directly and [self fileopened] but
            // bear in mind the fileListController will not be correctly set up when we return to it.

            [_document closeWithCompletionHandler:^(BOOL success){
                if (!success) {
                    // Handle the error.
                    LOG(@" error closing file after creation :-(");
                    FLOG(@" file URL is %@", [fileURL path]);
                } else {
                    FLOG(@" file closed %@", fileName);

                    _creatingNewFile = NO;  // OK we are done, so let metaData scanning go ahead as normal

                    // Tell our UITableView file list that we are done and trigger scanning of local and iCloud files
                    // The fileListController will the add the new file itself and the user will then pick the
                    // file from this list in order to open it.

                    // To open the file automatically use the callback in the fileListController
                    // to select and then open the file so it looks seamless to the user.
                    [self.fileListController fileHasBeenCreated:fileURL];


                    // Stop observing now
                    [center removeObserver:self
                                      name:UIDocumentStateChangedNotification
                                    object:_document];

                }
            }];
        }];
    }
}

还有,创建一个UIManagedDocument的子类,这样你就可以获取错误信息了,如果你还没有的话。在子类中只需要加入以下内容即可。
@implementation OSManagedDocument


- (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
    LOG(@"Auto-Saving Document");
    return [super contentsForType:typeName error:outError];
}

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted
{
    FLOG(@" error: %@", error.localizedDescription);
    NSArray* errors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(errors != nil && errors.count > 0) {
        for (NSError *error in errors) {
            FLOG(@" Error: %@", error.userInfo);
        }
    } else {
        FLOG(@" error.userInfo = %@", error.userInfo);
    }
}
@end

这是获取设备上/Documents目录的方法。文件名将附加到此路径。
- (NSURL*)documentsDirectoryURL
{
    _dataDirectoryURL = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
    return [_dataDirectoryURL URLByAppendingPathComponent:@"Documents"];
}

这里有更多信息的链接 http://ossh.com.au/design-and-technology/software-development/uimanageddocument-icloud-integration/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接