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

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

问题描述

是否有可能使用我的CoreData模型在今天的扩展swift像在原来的应用程序?如果是,我如何创建NSManagedObjectContext?

我真的没有线索,在组标识符旁边,但不幸的是我不知道如何获得上下文。

在过去,我创建的应用程序与检查一开始我想使用CoreData,然后我得到了managedObjectContext通过我的AppDelegate ..但我怎么可以做一个扩展中的像这样的东西?






$ b <$ p> NSURL * storeURL = [[self applicationDocumentsDirectory] ​​URLByAppendingPathComponent:@HTWcampus.sqlite];

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

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

由于我的应用程序中的现有数据库已经走了(什么是伟大的,将数据库放在共享段中)。



但是如何在扩展中创建一个上下文的实例呢?



在扩展中我有这样的代码:

  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 (contentOfURL:modelURL)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel:model)
coordinator.addPersistentStoreWithType(NSSQLiteStoreType,configuration:nil,URL:storeURL,options:nil,error:nil)
context = NSManagedObjectContext()
context.persistentStoreCoordinator = coordinator
}

如果是,我如何得到我的NSManagedObject子类?我必须添加momd文件到扩展目标吗?

解决方案

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

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

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



Edit:



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




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



  2. 下选择目标成员资格下的两个目标
  3. 模型文件,然后在右侧面板的目标成员资格下选择两个目标




  4. 对所有托管对象子类重复同样的操作


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..

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).

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
}

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?

解决方案

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

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.

Edit:

To add your model and managed object subclasses:

  1. Make sure you have your app and extension targets

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

  3. Repeat the same with all your managed object subclasses

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

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