简体   繁体   English

SwiftUI 预览 Canvas 崩溃与核心数据

[英]SwiftUI Preview Canvas crashes with core data

This might be an extremely dumb question, so sorry in advance.这可能是一个非常愚蠢的问题,所以提前抱歉。

I'm playing with core data and SwiftUI and I'm tryna make a simple app to store notes, but I can't seem to get the previews working.我正在使用核心数据和 SwiftUI 并且我正在尝试制作一个简单的应用程序来存储笔记,但我似乎无法让预览工作。 The app runs fine on the simulator but crashes on the preview canvas.该应用程序在模拟器上运行良好,但在预览版 canvas 上崩溃。 I've read a lot of Reddit and StackOverflow questions but none of them is related to my problem.我已经阅读了很多 Reddit 和 StackOverflow 问题,但没有一个与我的问题有关。

I'm already passing the context variable to my views, and I'm 100% sure it's not caused because of that because the crash reports say Persistent store migration failed, missing mapping model .I know it says it requires a migration, but I didn't change any data model (it's a completely new project), and the app works completely fine on the simulator so I'm not sure if that's really the cause of the crash.我已经将context变量传递给我的视图,我 100% 确定这不是因为崩溃报告说Persistent store migration failed, missing mapping model 。我知道它说它需要迁移,但我没有更改任何数据 model (这是一个全新的项目),该应用程序在模拟器上运行良好,所以我不确定这是否真的是崩溃的原因。

All I can think of is that SwiftUI's preview canvas may share the same database with other projects and it's conflicting (which kinda makes sense since I've got other projects that also use core data, are also made in SwiftUI, and share the name of some entities).我能想到的是 SwiftUI 的预览版 canvas 可能与其他项目共享相同的数据库并且它是冲突的(这有点道理,因为我有其他项目也使用核心数据,也是在 SwiftUI 中制作的,并且共享名称一些实体)。

I had these same problems using Realm and I solved them by removing the Realm database file when using the preview canvas so every run would empty the database, and I didn't have any issue.我在使用 Realm 时遇到了同样的问题,我通过在使用预览版 canvas 时删除 Realm 数据库文件解决了这些问题,因此每次运行都会清空数据库,我没有任何问题。 Is this a good approach?这是一个好方法吗? Should I do this?我应该这样做吗?

I'd show my code but it's really just a ForEach that iterates through an entity called Note .我会展示我的代码,但它实际上只是一个ForEach迭代一个名为Note的实体。

Try to delete the preview canvas from here尝试从这里删除预览 canvas

~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/

then in command line run the following然后在命令行中运行以下

killall -9 com.apple.CoreSimulator.CoreSimulatorService

finally restart xCode最后重启 xCode

If you have used a simulator you will also need to delete the model's store at /Users/YOURNAME/Library/Application Support如果您使用过模拟器,您还需要在 /Users/YOURNAME/Library/Application Support 中删除模型的存储

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM