如何在今天的扩展(iOS)中访问 CoreData 模型 [英] How to access CoreData model in today extension (iOS)

查看:15
本文介绍了如何在今天的扩展(iOS)中访问 CoreData 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像在原始应用中一样快速地在今天的扩展中使用我的 CoreData 模型?如果是,我如何创建 NSManagedObjectContext?
除了组标识符之外,我真的不知道,但不幸的是我不知道如何获取上下文..
过去,我在创建应用程序时首先检查我想使用 CoreData,然后通过我的 AppDelegate 获得 managedObjectContext .. 但是我怎么能在扩展中做这样的事情呢?Apple 不提供相关信息..

Is it possible to work with my CoreData model in the today extension in swift like in the original app? If yes, how can I create the NSManagedObjectContext?
I really have no clue, beside the group-identifier, but unfortunatly I don't know how to get the context..
In the past I created apps with the check at the beginning that I want to use CoreData and then I got the managedObjectContext via my AppDelegate.. But how can I do somethink like that in an extension? Apple doesn't offer information about that..

我在 AppDelegate 中编辑了这一行:

I edited this line in AppDelegate:

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"HTWcampus.sqlite"];

到此(在将组包括到两个目标之后):

to this (after including the group to both targets):

NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.BenchR.TodayExtensionSharingDefaults"];
storeURL = [storeURL URLByAppendingPathComponent:@"HTWcampus.sqlite"];
NSLog(@"StoreURL2: %@", storeURL);

我的应用程序中现有的数据库消失了(这很棒,因为我认为将数据库放在共享段中是有效的).

With that the existing database in my app was gone (what is great, because I think it worked to put the database in the shared segment).

但是如何在扩展中创建我的上下文实例?以及如何访问我的 NSManagedObject 子类?

But how can I create an instance of my context in the extension? And how can I access my NSManagedObject-subclasses?

到目前为止,在扩展中我有这个代码:

In the extension I have this code so far:

var context: NSManagedObjectContext!

override func viewDidLoad() {
    super.viewDidLoad()

    var storeURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.BenchR.TodayExtensionSharingDefaults")
    storeURL = storeURL?.URLByAppendingPathComponent("HTWcampus.sqlite")
    let modelURL = NSBundle.mainBundle().URLForResource("HTWcampus", withExtension: "momd")
    let model = NSManagedObjectModel(contentsOfURL: modelURL)
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
    coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: nil)
    context = NSManagedObjectContext()
    context.persistentStoreCoordinator = coordinator
}

这样对吗?如果是,我怎样才能在那里获得我的 NSManagedObject-Subclasses?我是否必须将 momd 文件添加到扩展目标?如果是,我该怎么做?

Is this right? And if yes, how can I get my NSManagedObject-Subclasses in there? And do I have to add the momd-file to the extensions target? If yes, how can I do that?

推荐答案

您真正想要的是访问您的持久存储(很可能是 SQLite 数据库).为了实现这一点,您需要配置 App Groups 并确保您的主机应用程序使用您的共享容器配置 Core Data 堆栈(因此您的商店也可以在扩展中访问).类似的东西:

What you really want is to access your persistent store (most likely a SQLite database). In order to achieve that, you need to configure App Groups and make sure that your host app configures the Core Data stack using your shared container (so your store is accessible in extension as well). Something like:

    NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:YOUR_SECURITY_APP_GROUP].path;
    NSString *sqlitePath = [NSString stringWithFormat:@"%@/%@", containerPath, @"database.sqlite"];

然后在您的扩展中,只需使用共享容器中的数据库创建具有托管对象上下文的持久存储协调器.您可以通过扩展名共享您的模型 (.momd) 和托管对象子类,只需确保它们也包含在扩展目标中即可.

Then in your extension just create persistent store coordinator with managed object contexts using database in shared container. You can share your model (.momd) and managed object subclasses with extension just by making sure they are included in extension target as well.

要添加您的模型和托管对象子类:

To add your model and managed object subclasses:

  1. 确保您拥有应用和扩展程序目标

  1. Make sure you have your app and extension targets

点击您的模型文件,然后在右侧面板的Target Membership"下选择两个目标

Click on your model file, and select both targets under 'Target Membership' on right-hand panel

对所有托管对象子类重复相同的操作

Repeat the same with all your managed object subclasses

这篇关于如何在今天的扩展(iOS)中访问 CoreData 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆