【问题标题】:Adding CoreData into an existing project using Objective-C使用 Objective-C 将 CoreData 添加到现有项目中
【发布时间】:2017-02-15 07:57:27
【问题描述】:

我正在尝试使用 CoreData 为我的应用程序创建数据存储。据我所知,Xcode 8 CoreData 使用的是persistentContainer 而不是managedObjectContext

我创建了一个包含所需实体的数据模型,并从编辑器菜单创建了一个NSManagedObject 子类。

我的问题是当我想使用persistentContainer时,没有找到标识符。

#import "UserCredentials+CoreDataClass.h"

//Fetch all username to array
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"UserCredentials"];
NSError *requestError = nil;


//I couldn't find the persistent container even though I had imported my header file.
NSArray *usernames = [self.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&requestError];

我意识到我的 CoreDataClass 甚至根本没有 persistentContainer 属性。我可以在哪里声明,以便访问我的数据存储?

【问题讨论】:

  • 使用self.context executeFetchRequest:fetchRequest error:&requestError];NSManagedObjectContext 没有被完全删除,每个NSManagedObject 都应该存在于此。
  • @Ludovic,我正在使用 Objective C。
  • @iamhx 不是一个真正的答案,但你应该阅读这个(它很快但很容易转换为 ObjC):stackoverflow.com/questions/37956720/…
  • @iamhx 是的,我删除了我的评论,对不起,但这是你应该实现 persistentContainer 的方式
  • @Ludovic 不用担心。让我尝试阅读 swift 代码,看看我是否可以转换它...

标签: ios objective-c core-data


【解决方案1】:

我假设您在创建对象时选择了核心数据选项。您的对象上下文为空,因为它存储在 AppDelegate 中。因此,您需要从 appdelegate 获取上下文引用,如下所示。

NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;
NSArray *usernames = [context executeFetchRequest:fetchRequest error:&requestError];

【讨论】:

  • 我没有选择核心数据选项,因为我的项目是现有项目
  • 所以只需创建一个新项目并从 AppDelegate 复制 CoreData 代码并添加到您现有的项目中。我这么说是因为这是避免错误的简单方法。否则显示您用于 CoreData 的所有代码。
  • 酷!那行得通。现在我正在尝试从您提供的答案中获取 AppDelegate 的上下文参考,但是您的第一行似乎有一些语法错误。
  • 已解决 :) 必须导入 AppDelegate 头文件。谢谢!
【解决方案2】:

你应该创建属性

//.h 文件

@property (readonly, strong) NSPersistentContainer *persistentContainer;

//.m 文件

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"CoreDataModel"]; //e.g. CoreDataModel.xcdatamodeld
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // 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.

                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    RLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }

    return _persistentContainer;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多