gpt4 book ai didi

ios - Cocoa Touch 框架和核心数据

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:16 26 4
gpt4 key购买 nike

我正在尝试在我的 Cocoa Touch Framework 中使用 CoreData。我创建一个简单的项目作为例子: https://github.com/JakubMazur/SO41698466

基本上我是按步骤做的:

  1. 使用单元测试创​​建框架
  2. CoreData添加到项目someDataModel
  3. 用一些虚拟实体填充 CoreData 并将模块更改为 Current Product Module
  4. 然后我创建了 SomeClass 开始,它基本上被称为:

.

public class func entityCreation() {
Entity(context: CoreDataClass().persistentContainer.viewContext)
}

因此,使用这个 lazy 从 CoreData 自动生成的代码,这应该创建模型。

  1. 在测试中我写道:

.

func testExample() {
SomeClass.entityCreation()
}
  1. 并启动测试
  2. CoreDataClass 中,我在行下方放置了一个断点:let container = NSPersistentContainer(name: "someDataModel") 然后我在控制台中看到:

[error] error: Failed to load model named someDataModel CoreData: error: Failed to load model named someDataModel

所以我有两个问题:

  1. 是否可以在框架中包含核心数据
  2. 如果 1 为真,也许我应该使用不同的 NSPersistentContainer 名称?有一种方法可以检查我应该放在那里什么?

在这里下载一个项目:https://github.com/JakubMazur/SO41698466

在下面找到并添加了解决方案

let modelURL = Bundle(for: type(of: self)).url(forResource: "someDataModel", withExtension: "momd")! 
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
let container = NSPersistentContainer(name: "someDataModel", managedObjectModel: managedObjectModel!)

最佳答案

TLDR;

Apple 建议您在框架中继承 NSPersistentContainer。如果您这样做,那么将搜索定义子类的框架。

所以你可以简单地:

final class PersistentContainer: NSPersistentContainer { }

然后在您的框架中使用 PersistentContainer 而不是 NSPersistentContainer

说明

name 初始化器只搜索主包。

要在不同的框架中查找托管对象模型,您可以按照评论中的建议获取模型 URL。

但是,作为替代方案,您也可以在您的框架中子类化 NSPersistentContainer,然后 name 初始化程序将起作用。

以下是 WWDC 核心数据最佳实践视频的节选:

... let's say we want to factor our model layer into its own framework.

We can do that by creating a new framework target in Xcode and moving our code into it.

It's all super easy, but when we move our model into the new target, in the built product, targets move from the app into the new framework, which is what's supposed to happen, but now NSPersistentContainer doesn't know where to find our model anymore.

This is because it only checks the main bundle by default.

Why stop there?

Well, searching all of the app's bundles could get really slow for a complicated app and it's not a cost you want to pay every time you spin up a stack.

How do we fix this?

Well, we could resuscitate the model out of the framework bundle ourselves and use one of the container's other initializers, like one that takes an explicit managed object model, but NSPersistentContainer actually has a way for you to change which bundle it searches.

See, NSPersistentContainer knows when it's been subclassed and will use the type of the subclass as a hint when it looks for the model.

All we need to do to take advantage of this is to create a subclass.

It doesn't even need to have anything in it.

Then, any code setting up through the container that wants to use our model can just adopt that subclass and the persistent container will check in our frameworks bundle for our model instead.

https://asciiwwdc.com/2018/sessions/224

关于ios - Cocoa Touch 框架和核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com